forked from anspress/anspress
-
Notifications
You must be signed in to change notification settings - Fork 0
/
anspress-question-answer.php
636 lines (561 loc) · 18.4 KB
/
anspress-question-answer.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
<?php
/**
* The WordPress Question and Answer Plugin.
*
* The most advance community question and answer system for WordPress
*
* @author Rahul Aryan <[email protected]>
* @copyright 2014 AnsPress.io & Rahul Aryan
* @license GPL-3.0+ https://www.gnu.org/licenses/gpl-3.0.txt
* @link https://anspress.io
* @package AnsPress
*
* @wordpress-plugin
* Plugin Name: AnsPress Question Answer
* Plugin URI: https://anspress.io
* Description: The most advance community question and answer system for WordPress
* Donate link: https://goo.gl/ffainr
* Version: 4.1.15
* Author: Rahul Aryan
* Author URI: https://anspress.io
* License: GPL-3.0+
* License URI: https://www.gnu.org/licenses/gpl-3.0.txt
* Text Domain: anspress-question-answer
* Domain Path: /languages
* GitHub Plugin URI: anspress/anspress
*/
// If this file is called directly, abort.
if ( ! defined( 'WPINC' ) ) {
die;
}
// Define database version.
define( 'AP_DB_VERSION', 35 );
// Check if using required PHP version.
if ( version_compare( PHP_VERSION, '5.5' ) < 0 ) {
/**
* Checks PHP version before initiating AnsPress.
*/
function ap_admin_php_version__error() {
$class = 'notice notice-error';
$message = '<strong>' . __( 'AnsPress is not running!', 'anspress-question-answer' ) . '</strong><br />';
$message .= sprintf( __( 'Irks! At least PHP version 5.5 is required to run AnsPress. Current PHP version is %s. Please ask hosting provider to update your PHP version.', 'anspress-question-answer' ), PHP_VERSION );
printf( '<div class="%1$s"><p>%2$s</p></div>', esc_attr( $class ), esc_html( $message ) );
}
add_action( 'admin_notices', 'ap_admin_php_version__error' );
return;
}
if ( ! class_exists( 'AnsPress' ) ) {
/**
* Main AnsPress class.
*/
class AnsPress {
/**
* AnsPress version
*
* @access private
* @var string
*/
private $_plugin_version = '4.1.15';
/**
* Class instance
*
* @access public
* @static
* @var object
*/
public static $instance = null;
/**
* AnsPress pages
*
* @access public
* @var array All AnsPress pages
*/
public $pages;
/**
* AnsPress menu
*
* @access public
* @var array AnsPress menu
*/
public $menu;
/**
* AnsPress question loop
*
* @access public
* @var object AnsPress question query loop
*/
public $questions;
/**
* Current question.
*
* @var object
*/
public $current_question;
/**
* AnsPress answers loop.
*
* @var object Answer query loop
*/
public $answers;
/**
* Current answer.
*
* @var object
*/
public $current_answer;
/**
* The array of actions registered with WordPress.
*
* @since 1.0.0
* @access protected
* @var array The actions registered with WordPress to fire when the plugin loads.
*/
protected $actions;
/**
* The array of filters registered with WordPress.
*
* @since 1.0.0
* @access protected
* @var array The filters registered with WordPress to fire when the plugin loads.
*/
protected $filters;
/**
* AnsPress reputation events.
*
* @access public
* @var object
*/
public $reputation_events;
/**
* AnsPress user pages.
*
* @access public
* @var object
*/
public $user_pages;
/**
* AnsPress question rewrite rules.
*
* @var array
* @since 4.1.0
*/
public $question_rule = [];
/**
* The forms.
*
* @var array
* @since 4.1.0
*/
public $forms = [];
/**
* The activity object.
*
* @var void|object
* @since 4.1.2
*/
public $activity;
/**
* The session.
*
* @var AnsPress\Session
* @since 4.1.5
*/
public $session;
/**
* Initializes the plugin by setting localization, hooks, filters, and administrative functions.
*
* @access public
* @static
*
* @return instance
*/
public static function instance() {
if ( ! isset( self::$instance ) && ! ( self::$instance instanceof self ) ) {
self::$instance = new self();
self::$instance->setup_constants();
self::$instance->actions = array();
self::$instance->filters = array();
self::$instance->includes();
self::$instance->session = AnsPress\Session::init();
self::$instance->site_include();
self::$instance->ajax_hooks();
AnsPress_PostTypes::init();
/*
* Dashboard and Administrative Functionality
*/
if ( is_admin() ) {
require_once ANSPRESS_DIR . 'admin/anspress-admin.php';
require_once ANSPRESS_DIR . 'admin/class-list-table-hooks.php';
AnsPress_Admin::init();
AnsPress_Post_Table_Hooks::init();
}
new AnsPress_Process_Form();
/*
* Hooks for extension to load their codes after AnsPress is loaded.
*/
do_action( 'anspress_loaded' );
if ( class_exists( 'WP_CLI' ) ) {
WP_CLI::add_command( 'anspress', 'AnsPress_Cli' );
}
} // End if().
return self::$instance;
}
/**
* Setup plugin constants.
*
* @since 2.0.1
* @access private
*/
private function setup_constants() {
$plugin_dir = wp_normalize_path( plugin_dir_path( __FILE__ ) );
$constants = array(
'DS' => DIRECTORY_SEPARATOR,
'AP_VERSION' => $this->_plugin_version,
'ANSPRESS_DIR' => $plugin_dir,
'ANSPRESS_URL' => plugin_dir_url( __FILE__ ),
'ANSPRESS_WIDGET_DIR' => $plugin_dir . 'widgets/',
'ANSPRESS_THEME_DIR' => $plugin_dir . 'templates',
'ANSPRESS_THEME_URL' => plugin_dir_url( __FILE__ ) . 'templates',
'ANSPRESS_CACHE_DIR' => WP_CONTENT_DIR . '/cache/anspress',
'ANSPRESS_CACHE_TIME' => HOUR_IN_SECONDS,
'ANSPRESS_ADDONS_DIR' => $plugin_dir . 'addons',
);
foreach ( $constants as $k => $val ) {
if ( ! defined( $k ) ) {
define( $k, $val );
}
}
}
/**
* Include required files.
*
* @since 2.0.1
* @access private
*/
private function includes() {
require_once ANSPRESS_DIR . 'loader.php';
require_once ANSPRESS_DIR . 'includes/activity.php';
require_once ANSPRESS_DIR . 'includes/common-pages.php';
require_once ANSPRESS_DIR . 'includes/class-theme.php';
require_once ANSPRESS_DIR . 'includes/class-form-hooks.php';
require_once ANSPRESS_DIR . 'includes/options.php';
require_once ANSPRESS_DIR . 'includes/functions.php';
require_once ANSPRESS_DIR . 'includes/hooks.php';
require_once ANSPRESS_DIR . 'includes/question-loop.php';
require_once ANSPRESS_DIR . 'includes/answer-loop.php';
require_once ANSPRESS_DIR . 'includes/qameta.php';
require_once ANSPRESS_DIR . 'includes/qaquery.php';
require_once ANSPRESS_DIR . 'includes/qaquery-hooks.php';
require_once ANSPRESS_DIR . 'includes/post-types.php';
require_once ANSPRESS_DIR . 'includes/post-status.php';
require_once ANSPRESS_DIR . 'includes/votes.php';
require_once ANSPRESS_DIR . 'includes/views.php';
require_once ANSPRESS_DIR . 'includes/theme.php';
require_once ANSPRESS_DIR . 'includes/shortcode-basepage.php';
require_once ANSPRESS_DIR . 'includes/process-form.php';
require_once ANSPRESS_DIR . 'includes/rewrite.php';
require_once ANSPRESS_DIR . 'includes/deprecated.php';
require_once ANSPRESS_DIR . 'includes/flag.php';
require_once ANSPRESS_DIR . 'includes/shortcode-question.php';
require_once ANSPRESS_DIR . 'includes/akismet.php';
require_once ANSPRESS_DIR . 'includes/comments.php';
require_once ANSPRESS_DIR . 'includes/upload.php';
require_once ANSPRESS_DIR . 'includes/taxo.php';
require_once ANSPRESS_DIR . 'includes/reputation.php';
require_once ANSPRESS_DIR . 'includes/subscribers.php';
require_once ANSPRESS_DIR . 'includes/class-query.php';
require_once ANSPRESS_DIR . 'includes/class/class-activity-helper.php';
require_once ANSPRESS_DIR . 'includes/class/class-activity.php';
require_once ANSPRESS_DIR . 'includes/class/class-session.php';
require_once ANSPRESS_DIR . 'widgets/search.php';
require_once ANSPRESS_DIR . 'widgets/question_stats.php';
require_once ANSPRESS_DIR . 'widgets/questions.php';
require_once ANSPRESS_DIR . 'widgets/breadcrumbs.php';
require_once ANSPRESS_DIR . 'widgets/ask-form.php';
require_once ANSPRESS_DIR . 'lib/class-anspress-upgrader.php';
require_once ANSPRESS_DIR . 'lib/class-form.php';
require_once ANSPRESS_DIR . 'lib/form/class-field.php';
require_once ANSPRESS_DIR . 'lib/form/class-input.php';
require_once ANSPRESS_DIR . 'lib/form/class-group.php';
require_once ANSPRESS_DIR . 'lib/form/class-repeatable.php';
require_once ANSPRESS_DIR . 'lib/form/class-checkbox.php';
require_once ANSPRESS_DIR . 'lib/form/class-select.php';
require_once ANSPRESS_DIR . 'lib/form/class-editor.php';
require_once ANSPRESS_DIR . 'lib/form/class-upload.php';
require_once ANSPRESS_DIR . 'lib/form/class-tags.php';
require_once ANSPRESS_DIR . 'lib/form/class-radio.php';
require_once ANSPRESS_DIR . 'lib/form/class-textarea.php';
require_once ANSPRESS_DIR . 'lib/class-validate.php';
require_once ANSPRESS_DIR . 'lib/class-wp-async-task.php';
require_once ANSPRESS_DIR . 'includes/class-async-tasks.php';
if ( defined( 'WP_CLI' ) && WP_CLI ) {
require_once ANSPRESS_DIR . 'lib/class-anspress-cli.php';
}
}
/**
* Register ajax hooks
*
* @access public
*/
public function ajax_hooks() {
// Load ajax hooks only if DOING_AJAX defined.
if ( defined( 'DOING_AJAX' ) && DOING_AJAX ) {
require_once ANSPRESS_DIR . 'admin/ajax.php';
require_once ANSPRESS_DIR . 'includes/ajax-hooks.php';
AnsPress_Ajax::init();
AnsPress_Admin_Ajax::init();
}
}
/**
* Include all public classes
*
* @access public
* @since 0.0.1
* @since 4.1.8 Load all addons if constant `ANSPRESS_ENABLE_ADDONS` is set.
*/
public function site_include() {
$this->theme_compat = new stdClass(); // Base theme compatibility class.
$this->theme_compat->active = false;
\AnsPress_Hooks::init();
$this->activity = AnsPress\Activity_Helper::get_instance();
\AnsPress_Views::init();
// Load all addons if constant set.
if ( defined( 'ANSPRESS_ENABLE_ADDONS' ) && ANSPRESS_ENABLE_ADDONS ) {
foreach ( ap_get_addons() as $name => $data ) {
ap_activate_addon( $name );
}
}
foreach ( (array) ap_get_addons() as $data ) {
if ( $data['active'] && file_exists( $data['path'] ) ) {
require_once $data['path'];
}
}
}
/**
* Add a new action to the collection to be registered with WordPress.
*
* @since 2.4
* @access public
*
* @param string $hook The name of the WordPress action that is being registered.
* @param object $component A reference to the instance of the object on which the action is defined.
* @param string $callback The name of the function definition on the $component.
* @param int Optional $priority The priority at which the function should be fired.
* @param int Optional $accepted_args The number of arguments that should be passed to the $callback.
*/
public function add_action( $hook, $component, $callback, $priority = 10, $accepted_args = 1 ) {
$this->actions = $this->add( $this->actions, $hook, $component, $callback, $priority, $accepted_args );
}
/**
* Add a new filter to the collection to be registered with WordPress.
*
* @since 2.4
* @access public
*
* @param string $hook The name of the WordPress filter that is being registered.
* @param object $component A reference to the instance of the object on which the filter is defined.
* @param string $callback The name of the function definition on the $component.
* @param int Optional $priority The priority at which the function should be fired.
* @param int Optional $accepted_args The number of arguments that should be passed to the $callback.
*/
public function add_filter( $hook, $component, $callback, $priority = 10, $accepted_args = 1 ) {
$this->filters = $this->add( $this->filters, $hook, $component, $callback, $priority, $accepted_args );
}
/**
* A utility function that is used to register the actions and hooks into a single
* collection.
*
* @since 2.4
* @access private
*
* @param array $hooks The collection of hooks that is being registered (that is, actions or filters).
* @param string $hook The name of the WordPress filter that is being registered.
* @param object $component A reference to the instance of the object on which the filter is defined.
* @param string $callback The name of the function definition on the $component.
* @param int Optional $priority The priority at which the function should be fired.
* @param int Optional $accepted_args The number of arguments that should be passed to the $callback.
* @param integer $priority Priority.
* @param integer $accepted_args Accepted arguments.
*
* @return type The collection of actions and filters registered with WordPress.
*/
private function add( $hooks, $hook, $component, $callback, $priority, $accepted_args ) {
$hooks[] = array(
'hook' => $hook,
'component' => $component,
'callback' => $callback,
'priority' => $priority,
'accepted_args' => $accepted_args,
);
return $hooks;
}
/**
* Register the filters and actions with WordPress.
*
* @access public
*/
public function setup_hooks() {
foreach ( $this->filters as $hook ) {
add_filter( $hook['hook'], array( $hook['component'], $hook['callback'] ), $hook['priority'], $hook['accepted_args'] );
}
foreach ( $this->actions as $hook ) {
add_action( $hook['hook'], array( $hook['component'], $hook['callback'] ), $hook['priority'], $hook['accepted_args'] );
}
}
/**
* Get specific AnsPress form.
*
* @param string $name Name of form.
* @return false|object
* @since 4.1.0
*/
public function &get_form( $name ) {
$name = preg_replace( '/^form_/i', '', $name );
if ( $this->form_exists( $name ) ) {
return $this->forms[ $name ];
}
return false;
}
/**
* Check if a form exists in AnsPress, if not then tries to register.
*
* @param string $name Name of form.
* @return boolean
* @since 4.1.0
*/
public function form_exists( $name ) {
$name = preg_replace( '/^form_/i', '', $name );
if ( isset( $this->forms[ $name ] ) ) {
return true;
}
/**
* Register a form in AnsPress.
*
* @param array $form {
* Form options and fields. Check @see `AnsPress\Form` for more detail.
*
* @type string $submit_label Custom submit button label.
* @type boolean $editing Pass true if currently in editing mode.
* @type integer $editing_id If editing then pass editing post or comment id.
* @type array $fields Fields. For more detail on field option check documentations.
* }
* @since 4.1.0
* @todo Add detailed docs for `$fields`.
*/
$args = apply_filters( 'ap_form_' . $name, null );
if ( ! is_null( $args ) && ! empty( $args ) ) {
$this->forms[ $name ] = new AnsPress\Form( 'form_' . $name, $args );
return true;
}
return false;
}
}
} // End if().
/**
* Run AnsPress thingy
*
* @return object
*/
if ( ! function_exists( 'anspress' ) ) {
/**
* Initialize AnsPress.
*/
function anspress() {
return AnsPress::instance();
}
}
if ( ! class_exists( 'AnsPress_Init' ) ) {
/**
* AnsPress initialization class.
*/
class AnsPress_Init {
/**
* Load anspress.
*
* @access public
* @static
*/
public static function load_anspress() {
/*
* Action before loading AnsPress.
* @since 2.4.7
*/
do_action( 'before_loading_anspress' );
anspress()->setup_hooks();
}
/**
* Load translations.
*
* @since 2.0.1
* @access public
* @static
*/
public static function load_textdomain() {
$locale = apply_filters( 'plugin_locale', get_locale(), 'anspress-question-answer' );
$loaded = load_textdomain( 'anspress-question-answer', trailingslashit( WP_LANG_DIR ) . "anspress-question-answer/anspress-question-answer-{$locale}.mo" );
if ( $loaded ) {
return $loaded;
} else {
load_plugin_textdomain( 'anspress-question-answer', false, basename( dirname( __FILE__ ) ) . '/languages/' );
}
}
/**
* Creating table whenever a new blog is created
*
* @access public
* @static
*
* @param integer $blog_id Blog id.
* @param integer $user_id User id.
* @param string $domain Domain.
* @param string $path Path.
* @param integer $site_id Site id.
* @param array $meta Site meta.
*/
public static function create_blog( $blog_id, $user_id, $domain, $path, $site_id, $meta ) {
if ( is_plugin_active_for_network( plugin_basename( __FILE__ ) ) ) {
switch_to_blog( $blog_id ); // @codingStandardsIgnoreLine
require_once dirname( __FILE__ ) . '/activate.php';
AP_Activate::get_instance( true );
restore_current_blog();
}
}
/**
* Deleting the table whenever a blog is deleted
*
* @access public
* @static
*
* @param array $tables Table names.
* @param int $blog_id Blog ID.
*
* @return array
*/
public static function drop_blog_tables( $tables, $blog_id ) {
if ( empty( $blog_id ) || 1 === (int) $blog_id || $blog_id !== $GLOBALS['blog_id'] ) {
return $tables;
}
global $wpdb;
$tables[] = $wpdb->prefix . 'ap_views';
$tables[] = $wpdb->prefix . 'ap_qameta';
$tables[] = $wpdb->prefix . 'ap_activity';
$tables[] = $wpdb->prefix . 'ap_votes';
return $tables;
}
}
} // End if().
add_action( 'plugins_loaded', [ 'AnsPress_Init', 'load_anspress' ], 1 );
add_action( 'plugins_loaded', [ 'AnsPress_Init', 'load_textdomain' ], 0 );
add_action( 'wpmu_new_blog', [ 'AnsPress_Init', 'create_blog' ], 10, 6 );
add_filter( 'wpmu_drop_tables', [ 'AnsPress_Init', 'drop_blog_tables' ], 10, 2 );
require_once dirname( __FILE__ ) . '/includes/class/roles-cap.php';
require_once dirname( __FILE__ ) . '/includes/class/class-singleton.php';
/*
* Register hooks that are fired when the plugin is activated or deactivated.
* When the plugin is deleted, the uninstall.php file is loaded.
*/
function anspress_activation() {
require_once dirname( __FILE__ ) . '/activate.php';
\AP_Activate::get_instance();
}
register_activation_hook( __FILE__, 'anspress_activation' );