PluginProbe
wpForo Forum / 2.0.5
wpForo Forum v2.0.5
3.1.5 3.1.4 3.1.2 3.1.1 3.1.0 3.0.9 3.0.8 3.0.7 trunk 1.0.0 1.0.1 1.0.2 1.1.0 1.1.1 1.1.2 1.2.0 1.3.0 1.3.1 1.4.0 1.4.1 1.4.10 1.4.11 1.4.12 1.4.13 1.4.2 All 137 releases
wpforo / wpforo.php

wpforo.php in wpForo Forum 2.0.5, at wpforo.php

1,293 lines 49.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /*
3 * Plugin Name: wpForo
4 * Plugin URI: https://wpforo.com
5 * Description: WordPress Forum plugin. wpForo is a full-fledged forum solution for your community. Comes with multiple modern forum layouts.
6 * Author: gVectors Team
7 * Author URI: https://gvectors.com/
8 * Version: 2.0.5
9 * Text Domain: wpforo
10 * Domain Path: /languages
11 */
12
13 namespace wpforo;
14
15 define( 'WPFORO_VERSION', '2.0.5' );
16
17 //Exit if accessed directly
18 if( ! defined( 'ABSPATH' ) ) exit;
19
20 define( 'WPFORO_DIR', rtrim( plugin_dir_path( __FILE__ ), '/' ) );
21 define( 'WPFORO_URL', rtrim( plugins_url( '', __FILE__ ), '/' ) );
22 define( 'WPFORO_BASENAME', plugin_basename( __FILE__ ) ); //wpforo/wpforo.php
23
24 require_once WPFORO_DIR . "/autoload.php";
25
26 use stdClass;
27 use wpdb;
28 use wpforo\classes\Actions;
29 use wpforo\classes\Activity;
30 use wpforo\classes\API;
31 use wpforo\classes\Boards;
32 use wpforo\classes\Cache;
33 use wpforo\classes\Feed;
34 use wpforo\classes\Forms;
35 use wpforo\classes\Forums;
36 use wpforo\classes\Logs;
37 use wpforo\classes\Members;
38 use wpforo\classes\Moderation;
39 use wpforo\classes\Notices;
40 use wpforo\classes\Permissions;
41 use wpforo\classes\Phrases;
42 use wpforo\classes\PostMeta;
43 use wpforo\classes\Posts;
44 use wpforo\classes\RamCache;
45 use wpforo\classes\SEO;
46 use wpforo\classes\Settings;
47 use wpforo\classes\Template;
48 use wpforo\classes\Topics;
49 use wpforo\classes\UserGroups;
50 use wpforo\modules\bookmarks\Bookmarks;
51 use wpforo\modules\revisions\Revisions;
52 use wpforo\modules\reactions\Reactions;
53 use wpforo\modules\subscriptions\Subscriptions;
54
55 final class wpforo {
56 private static $_instance = null;
57
58 /** @var wpdb * */
59 public $db;
60 public $default;
61
62 public $blog_prefix = "wp_";
63 public $base_prefix = "wpforo_";
64 public $prefix = "wpforo_";
65 public $_base_tables = [
66 'boards',
67 'usergroups',
68 'profiles',
69 'logs',
70 'follows',
71 'bookmarks',
72 'accesses',
73 ];
74 public $_tables = [
75 'activity',
76 'forums',
77 'languages',
78 'phrases',
79 'postmeta',
80 'posts',
81 'post_revisions',
82 'subscribes',
83 'topics',
84 'tags',
85 'visits',
86 'reactions',
87 ];
88 public $tables;
89 private $_folders = [
90 'assets',
91 'attachments',
92 'avatars',
93 'covers',
94 'cache',
95 'default_attachments',
96 'emoticons',
97 ];
98 public $folders = [];
99
100 public $current_url;
101 public $GET = [];
102 public $current_object;
103
104 public $wp_current_user = null;
105 public $current_user = [];
106 public $current_usermeta = [];
107 public $current_user_groupid = 4;
108 public $current_user_groupids = [ 4 ];
109 public $current_user_secondary_groupids = [];
110 public $current_userid = 0;
111 public $current_user_login = '';
112 public $current_user_email = '';
113 public $current_user_display_name = '';
114 public $current_user_status = '';
115 public $current_user_accesses = [];
116 public $session_token = '';
117
118 public $tools_cleanup;
119 public $tools_misc;
120 public $locale = 'en_US';
121
122 public $dissmissed;
123
124 /** @var Settings */
125 public $settings;
126 /** @var Actions */
127 public $action;
128 /** @var Activity */
129 public $activity;
130 /** @var API */
131 public $api;
132 /** @var Boards */
133 public $board;
134 /** @var Cache */
135 public $cache;
136 /** @var Feed */
137 public $feed;
138 /** @var Forms */
139 public $form;
140 /** @var Forums */
141 public $forum;
142 /** @var Logs */
143 public $log;
144 /** @var Members */
145 public $member;
146 /** @var Moderation */
147 public $moderation;
148 /** @var Notices */
149 public $notice;
150 /** @var Permissions */
151 public $perm;
152 /** @var Phrases */
153 public $phrase;
154 /** @var PostMeta */
155 public $postmeta;
156 /** @var Posts */
157 public $post;
158 /** @var RamCache */
159 public $ram_cache;
160 /** @var Revisions */
161 public $revision;
162 /** @var Reactions */
163 public $reaction;
164 /** @var Bookmarks */
165 public $bookmark;
166 /** @var SEO */
167 public $seo;
168 /** @var Subscriptions */
169 public $sbscrb;
170 /** @var Template */
171 public $tpl;
172 /** @var Topics */
173 public $topic;
174 /** @var UserGroups */
175 public $usergroup;
176
177 public static function instance() {
178 if( is_null( self::$_instance ) ) self::$_instance = new self();
179
180 return self::$_instance;
181 }
182
183 private function init_db() {
184 global $wpdb;
185 $this->db = $wpdb;
186 }
187
188 public function is_db_mysql8() {
189 if( preg_match( '#([\d.]+)-MariaDB#iu', $this->db->get_var( "SELECT VERSION()" ), $match ) ) {
190 $use_mysql8 = version_compare( $match[1], '10.2.6', '>=' );
191 } else {
192 $use_mysql8 = version_compare( $this->db->db_version(), '8.0.0', '>=' );
193 }
194
195 return apply_filters( 'wpforo_use_mysql8', $use_mysql8 );
196 }
197
198 private function __construct() {
199 $this->init_db();
200 $this->init_hooks();
201 }
202
203 public function generate_prefix( $boardid ) {
204 $boardid = intval( $boardid );
205
206 return $this->base_prefix . ( $boardid ? $boardid . '_' : '' );
207 }
208
209 private function init_prefix() {
210 $boardid = $this->board->get_current( 'boardid' );
211 $this->prefix = $this->generate_prefix( $boardid );
212 }
213
214 public function change_board( $args = [] ) {
215 do_action( 'wpforo_before_change_board', $args );
216
217 $boardid = $this->board->get_current( 'boardid' );
218 $is_inited = (bool) wpfval( $this->board->is_inited, $boardid );
219 $this->board->init_current( $args );
220
221 if( ! $is_inited || $boardid !== $this->board->get_current( 'boardid' ) ) {
222 $this->ram_cache->reset();
223
224 $this->init_prefix();
225 $this->init_tables();
226 $this->init_folders();
227 $this->init_options();
228
229 do_action( 'wpforo_after_change_board', $args );
230 }
231 }
232
233 private function init_base_classes() {
234 $this->requires();
235 $this->init_defaults();
236 $this->reset_current_object();
237 $this->init_base_tables();
238 $this->init_folders();
239
240 do_action( 'wpforo_before_init_base_classes' );
241
242 $this->settings = new Settings();
243 $this->tpl = new Template();
244 $this->ram_cache = new RamCache();
245 $this->cache = new Cache();
246 $this->action = new Actions();
247 $this->board = new Boards();
248 $this->usergroup = new UserGroups();
249 $this->member = new Members();
250 $this->perm = new Permissions();
251 $this->notice = new Notices();
252
253 $this->moderation = new Moderation();
254 $this->phrase = new Phrases();
255
256 do_action( 'wpforo_after_init_base_classes' );
257 }
258
259 private function init_classes() {
260 do_action( 'wpforo_before_init_classes' );
261
262 $this->activity = new Activity();
263 $this->api = new API();
264 $this->feed = new Feed();
265 $this->form = new Forms();
266 $this->forum = new Forums();
267 $this->log = new Logs();
268 $this->postmeta = new PostMeta();
269 $this->post = new Posts();
270 $this->seo = new SEO();
271 $this->topic = new Topics();
272 $this->reaction = new Reactions();
273 if( wpforo_is_module_enabled( 'bookmarks' ) ) $this->bookmark = new Bookmarks();
274 if( wpforo_is_module_enabled( 'revisions' ) ) $this->revision = new Revisions();
275 if( wpforo_is_module_enabled( 'subscriptions' ) ) $this->sbscrb = new Subscriptions();
276
277 do_action( 'wpforo_after_init_classes' );
278 }
279
280 public function set_locale( $locale ) {
281 $this->locale = $locale;
282 do_action( 'wpforo_after_set_locale', $locale );
283 }
284
285 private function init_hooks() {
286 add_action(
287 'after_setup_theme',
288 function() {
289 $this->init_base_classes();
290 if( ! $this->is_installed() ) $this->init();
291 },
292 0
293 );
294 add_action( 'change_locale', [ $this, 'set_locale' ] );
295 add_action( 'init', function() { $this->set_locale( get_locale() ); } );
296 add_action( 'widgets_init', function() {
297 $boards = $this->board->get_boards( [ 'status' => true ] );
298 foreach( $boards as $board ) {
299 register_sidebar(
300 [
301 'id' => sprintf(
302 'wpforo_%1$ssidebar',
303 $board['boardid'] ? $board['boardid'] . '_' : ''
304 ),
305 'name' => __( 'wpForo Sidebar', 'wpforo' ) . ' - ( ' . $board['title'] . ' )',
306 'description' => __( "NOTE: If you're going to add widgets in this sidebar, please use 'Full Width' template for wpForo index page to avoid sidebar duplication.", 'wpforo' ),
307 'before_widget' => '<aside id="%1$s" class="footer-widget-col %2$s clearfix">',
308 'after_widget' => '</aside>',
309 'before_title' => '<h3 class="widget-title">',
310 'after_title' => '</h3>',
311 ]
312 );
313 }
314
315 register_widget( 'wpforo\widgets\Forums' );
316 register_widget( 'wpforo\widgets\Profile' );
317 register_widget( 'wpforo\widgets\Search' );
318 register_widget( 'wpforo\widgets\OnlineMembers' );
319 register_widget( 'wpforo\widgets\RecentTopics' );
320 register_widget( 'wpforo\widgets\RecentPosts' );
321 register_widget( 'wpforo\widgets\Tags' );
322 } );
323 add_filter( 'plugin_locale', function( $locale, $domain ) {
324 if( $domain === 'wpforo' && defined( 'DOING_AJAX' ) && DOING_AJAX ) $locale = $this->locale;
325
326 return $locale;
327 }, 10, 2 );
328 add_action( 'wpforo_after_init_classes', function() {
329 if( wpforo_setting( 'email', 'disable_new_user_admin_notification' ) ) {
330 remove_action( 'after_password_reset', 'wp_password_change_notification' );
331
332 remove_action( 'register_new_user', 'wp_send_new_user_notifications' );
333 add_action( 'register_new_user', 'wpforo_send_new_user_notifications' );
334
335 remove_action( 'edit_user_created_user', 'wp_send_new_user_notifications' );
336 add_action( 'edit_user_created_user', 'wpforo_send_new_user_notifications', 10, 2 );
337
338 remove_action( 'network_site_new_created_user', 'wp_send_new_user_notifications' );
339 add_action( 'network_site_new_created_user', 'wpforo_send_new_user_notifications' );
340
341 remove_action( 'network_site_users_created_user', 'wp_send_new_user_notifications' );
342 add_action( 'network_site_new_created_user', 'wpforo_send_new_user_notifications' );
343
344 remove_action( 'network_user_new_created_user', 'wp_send_new_user_notifications' );
345 add_action( 'network_user_new_created_user', 'wpforo_send_new_user_notifications' );
346 }
347 } );
348
349 if( is_admin() || strpos( $_SERVER['REQUEST_URI'], '/wp-json/' ) === 0 || preg_match( '#^/?(?:([^\s/?&=<>:\'\"*\\\|]*/)(?1)*)?[^\s/?&=<>:\'\"*\\\|]+\.(?:php|js|css|jpe?g|png|gif)/?(?:\?[^/]*)?$#iu', $_SERVER['REQUEST_URI'] ) ) {
350 add_action( 'init', [ $this, 'init' ], 99 );
351 add_action( 'admin_init', [ $this, 'admin_init' ] );
352 } else {
353 add_action( 'wp', [ $this, 'init' ], 99 );
354 }
355 add_action( 'switch_blog', [ $this, 'after_switch_blog' ], 10, 2 );
356 add_action( 'wpforo_before_init', [ $this, 'change_board' ] );
357 add_action( 'wpforo_after_init_current_url', [ $this, 'change_board' ] );
358 }
359
360 public function after_switch_blog( $new_blog_id, $prev_blog_id ) {
361 if( intval( $new_blog_id ) !== intval( $prev_blog_id ) ) {
362 $this->init_base_tables();
363 $this->init_tables();
364 $this->init_folders();
365 }
366 }
367
368 private function init_base_tables() {
369 $this->db->query( "SET SESSION group_concat_max_len = 1000000" );
370 $blog_id = apply_filters( 'wpforo_current_blog_id', 0 );
371 $this->tables = new stdClass;
372 if( ! $blog_id ) $blog_id = $this->db->blogid;
373 $this->blog_prefix = $this->db->get_blog_prefix( $blog_id );
374 $this->_base_tables = apply_filters( 'wpforo_init_base_tables', $this->_base_tables );
375 foreach( $this->_base_tables as $table ) $this->tables->$table = $this->fix_table_name( $table );
376 }
377
378 private function init_tables() {
379 $this->_tables = apply_filters( 'wpforo_init_tables', $this->_tables );
380 foreach( $this->_tables as $table ) $this->tables->$table = $this->fix_table_name( $table );
381 }
382
383 public function get_active_boards_tables( $basename ) {
384 $tables = [];
385 if( $boardids = $this->board->get_active_boardids() ) {
386 foreach( $boardids as $boardid ) $tables[] = $this->fix_table_name( $basename, $this->generate_prefix( $boardid ) );
387 }
388
389 return array_unique( $tables );
390 }
391
392 /**
393 * @param string $basename
394 *
395 * @return string
396 */
397 public function fix_table_name( $basename, $prefix = null ) {
398 return $this->blog_prefix . ( in_array( $basename, $this->_base_tables, true ) ? $this->base_prefix : ( is_null( $prefix ) ? $this->prefix : $prefix ) ) . $basename;
399 }
400
401 public function fix_dir_sep( $dir ) {
402 $dir = str_replace( [ '/', '\\', '\\\\' ], DIRECTORY_SEPARATOR, $dir );
403
404 return rtrim( trim( $dir ), DIRECTORY_SEPARATOR );
405 }
406
407 public function fix_url_sep( $url ) {
408 return trim( str_replace( [ '/', '\\', '\\\\' ], '/', $url ) );
409 }
410
411 private function init_folders() {
412 $this->_folders = apply_filters( 'wpforo_init_folders', $this->_folders );
413 $boardid = ( is_callable( [ $this->board, 'get_current' ] ) ? $this->board->get_current( 'boardid' ) : 0 );
414 $upload_dir = wp_get_upload_dir();
415 $this->folders['wp_upload'] = [
416 'dir' => $this->fix_dir_sep( $upload_dir['basedir'] ),
417 'url' => $this->fix_url_sep( $upload_dir['baseurl'] ),
418 ];
419 $this->folders['wp_upload']['url//'] = preg_replace( '#^https?:#i', '', $this->folders['wp_upload']['url'] );
420 $this->folders['upload'] = [
421 'dir' => $this->folders['wp_upload']['dir'] . DIRECTORY_SEPARATOR . "wpforo" . ( $boardid ? '_' . $boardid : '' ),
422 'url' => $this->folders['wp_upload']['url'] . "/wpforo" . ( $boardid ? '_' . $boardid : '' ),
423 'url//' => $this->folders['wp_upload']['url//'] . "/wpforo" . ( $boardid ? '_' . $boardid : '' ),
424 ];
425 foreach( $this->_folders as $folder ) {
426 $this->folders[ $folder ] = [
427 'dir' => $this->folders['upload']['dir'] . DIRECTORY_SEPARATOR . trim( $this->fix_dir_sep( $folder ), DIRECTORY_SEPARATOR ),
428 'url' => $this->folders['upload']['url'] . "/" . trim( $this->fix_url_sep( $folder ), '/' ),
429 'url//' => $this->folders['upload']['url//'] . "/" . trim( $this->fix_url_sep( $folder ), '/' ),
430 ];
431 }
432 do_action( 'wpforo_after_init_folders', $this->folders );
433 }
434
435 private function requires() {
436 require_once WPFORO_DIR . '/includes/functions.php';
437 require_once WPFORO_DIR . '/includes/functions-template.php';
438 require_once WPFORO_DIR . '/includes/hooks.php';
439 require_once WPFORO_DIR . '/includes/installation.php';
440 require_once WPFORO_DIR . '/integrations/functions.php';
441 if( wpforo_is_admin() ) require_once WPFORO_DIR . '/admin/index.php';
442 }
443
444 public function admin_init() {
445 if( wpforo_is_admin() ) {
446 $this->change_board();
447
448 $this->check_issues();
449
450 if( strpos( wpforo_get_request_uri(), 'user-new.php' ) === false ) {
451 if( ! $this->member->get_groupid( $this->current_userid ) ) {
452 $this->member->synchronize_user( $this->current_userid );
453 }
454 }
455
456 if( ! $this->usergroup->can_manage_forum() && wpforo_current_user_is( 'admin' ) ) {
457 $this->member->set_groupid( $this->current_userid, 1 );
458 }
459 }
460 }
461
462 public function check_issues() {
463 //Make sure all users profiles are created
464 if( $this->is_installed() ) {
465 $users = (int) $this->db->get_var( "SELECT COUNT(*) FROM `" . $this->db->users . "`" );
466 $profiles = (int) $this->db->get_var( "SELECT COUNT(*) FROM `" . $this->tables->profiles . "`" );
467 $delta = $users - $profiles;
468 if( apply_filters( 'wpforo_profile_synchronization_notice', true ) ) {
469 if( true ) add_action( 'admin_notices', 'wpforo_profile_notice', 10 );
470 }
471 }
472 //Make sure tables structures are correct for current version
473 $wpforo_version_db = wpforo_get_option( 'version_db', null, false );
474 if( ! $wpforo_version_db || version_compare( $wpforo_version_db, WPFORO_VERSION, '<' ) || wpforo_setting( 'general', 'debug_mode' ) ) {
475 if( 'tables' !== wpfval( $_GET, 'view' ) ) {
476 $db_note = false;
477 $problems = wpforo_database_check();
478 if( ! empty( $problems ) ) {
479 foreach( $problems as $problem ) {
480 if( wpfval( $problem, 'fields' ) ) $db_note = true;
481 if( wpfval( $problem, 'exists' ) ) $db_note = true;
482 }
483 if( $db_note ) {
484 add_action( 'admin_notices', 'wpforo_database_notice', 10 );
485 }
486 } else {
487 wpforo_update_option( 'version_db', WPFORO_VERSION );
488 }
489 }
490 }
491 //Check cache plugins
492 $not_excluded_plugins = WPF()->cache->cache_plugins_status();
493 if( ! empty( $not_excluded_plugins ) ) {
494 add_action( 'admin_notices', 'wpforo_cache_information', 10 );
495 }
496 }
497
498 public function init() {
499 do_action( 'wpforo_before_init' );
500 $this->init_classes();
501 $this->init_current_url();
502 do_action( 'wpforo_core_inited' );
503 $this->init_current_object();
504 do_action( 'wpforo_after_init' );
505 }
506
507 public function shortcode_atts_to_url( $atts ) {
508 $url = wpforo_home_url();
509
510 $args = shortcode_atts(
511 [
512 'item' => 'forum',
513 'id' => 0,
514 'slug' => '',
515 ],
516 (array) $atts
517 );
518
519 if( $args['item'] === 'profile' && ! $args['id'] ) $args['id'] = $this->current_userid;
520
521 if( $args['item'] === 'add-topic' ) {
522 $forum = $this->forum->get_forum( ( $args['slug'] ?: $args['id'] ) );
523 $forumid = (int) wpfval( $forum, 'is_cat' ) ? 0 : (int) wpfval( $forum, 'forumid' );
524 $url = wpforo_home_url( wpforo_settings_get_slug( 'add-topic' ) . '/' . $forumid );
525 } elseif( $args['item'] === 'recent' ) {
526 $url = wpforo_settings_get_slug( 'recent' ) . '/';
527 if( trim( $args['slug'] ) ) $url .= '?view=' . wpforo_settings_get_slug( $args['slug'] );
528 $url = wpforo_home_url( $url );
529 } elseif( $args['id'] || $args['slug'] ) {
530 $getid = ( $args['slug'] ?: $args['id'] );
531 if( $args['item'] === 'topic' ) {
532 $url = $this->topic->get_topic_url( $getid );
533 } elseif( $args['item'] === 'profile' ) {
534 $url = $this->member->get_profile_url( $getid );
535 } else {
536 $url = $this->forum->get_forum_url( $getid );
537 }
538 } elseif( $args['item'] === 'login' ) {
539 $url = wpforo_login_url();
540 } elseif( $args['item'] === 'register' ) {
541 $url = wpforo_register_url();
542 } elseif( $args['item'] === 'lostpassword' ) {
543 $url = wpforo_lostpassword_url();
544 }
545
546 return $url;
547 }
548
549 public function init_current_url( $atts = [] ) {
550 if( is_scalar( $atts ) ) {
551 $url = $atts;
552 $atts = [];
553 } else {
554 $url = wpforo_get_request_uri();
555 }
556 if( $atts || is_wpforo_shortcode_page( $url ) ) {
557 if( $atts || ( $atts = get_wpforo_shortcode_atts( '', $url ) ) ) {
558 $url = $this->shortcode_atts_to_url( $atts );
559 } else {
560 $url = wpforo_home_url();
561 }
562 } elseif( is_wpforo_url( $url ) && preg_match( '#/' . preg_quote( wpforo_settings_get_slug( 'postid' ) ) . '/(\d+)/?$#isu', strtok( $url, '?' ), $matches ) ) {
563 $post_url = $this->post->get_post_url( $matches[1] );
564 if( $post_url !== wpforo_home_url() ) $url = $post_url;
565 }
566
567 if( function_exists( 'pll_default_language' ) ) {
568 $qvar = wpforo_get_url_query_vars_str( $url );
569 $qvar = preg_replace( '#^' . preg_quote( pll_default_language() ) . '/#', '', $qvar );
570 $url = $this->user_trailingslashit( home_url( $qvar ) );
571 }
572
573 $url = wpforo_fix_url( $url );
574 $url = preg_replace( '#\#[^/?&]*$#iu', '', $url );
575 parse_str( parse_url( $url, PHP_URL_QUERY ), $get );
576 $get = array_merge( (array) $get, $_GET );
577 $get = wp_unslash( $get );
578
579 $this->current_url = apply_filters( 'wpforo_init_current_url', $url );
580 $this->GET = apply_filters( 'wpforo_init_current_url_GET', $get );
581
582 do_action( 'wpforo_after_init_current_url', $atts );
583 }
584
585 private function init_defaults() {
586 date_default_timezone_set( 'UTC' );
587 ini_set( 'date.timezone', 'UTC' );
588
589 $this->default = new stdClass;
590
591 $this->default->current_object = [
592 'route' => '',
593 'template' => '',
594 'qvars' => [],
595 'args' => [],
596 'layout' => 1,
597 'og_text' => '',
598 'paged' => 1,
599 'items_count' => 0,
600 'items_per_page' => 15,
601 'is_404' => false,
602 'user_is_same_current_user' => false,
603 'orderby' => null,
604 'members' => [],
605 'categories' => [],
606 'topics' => [],
607 'posts' => [],
608 'user' => [],
609 'userid' => 0,
610 'user_nicename' => '',
611 'forum' => [],
612 'forumid' => 0,
613 'forum_slug' => '',
614 'forum_desc' => '',
615 'forum_meta_key' => '',
616 'forum_meta_desc' => '',
617 'topic' => [],
618 'topicid' => 0,
619 'topic_slug' => '',
620 'tags' => [],
621 'load_tinymce' => false,
622 ];
623
624 $this->default->tools_cleanup = [
625 'user_reg_days_ago' => 7,
626 'auto_cleanup_users' => 0,
627 'usergroup' => [ 1 => '0', 5 => '0', 2 => '1', 3 => '0' ],
628 ];
629
630 $this->default->tools_misc = [
631 'admin_note' => '',
632 'admin_note_groups' => [ 1, 2, 3, 4, 5 ],
633 'admin_note_pages' => [ 'forum' ],
634 ];
635
636 $this->default->stats = [
637 'forums' => 0,
638 'topics' => 0,
639 'posts' => 0,
640 'members' => 0,
641 'online_members_count' => 0,
642 'last_post_title' => '',
643 'last_post_url' => '',
644 'newest_member' => [],
645 'newest_member_dname' => '',
646 'newest_member_profile_url' => '',
647 ];
648
649 $this->default->dissmissed = [
650 'recaptcha_backend_note' => 0,
651 'recaptcha_note' => 0,
652 'addons_css_update' => 0,
653 ];
654 }
655
656 private function reset_current_object() {
657 $this->current_object = $this->default->current_object;
658 }
659
660 private function init_options() {
661 $this->tools_cleanup = wpforo_get_option( 'tools_cleanup', $this->default->tools_cleanup );
662 $this->tools_misc = wpforo_get_option( 'tools_misc', $this->default->tools_misc );
663 $this->dissmissed = wpforo_get_option( 'dissmissed', $this->default->dissmissed );
664 }
665
666 public function can_use_trailing_slashes() {
667 return '/' === substr( get_option( 'permalink_structure', '' ), - 1, 1 );
668 }
669
670 public function user_trailingslashit( $url ) {
671 $rtrimed_url = '';
672 $url_append_vars = '';
673 if( preg_match( '#^(.+?)(/?[?&].*)?$#isu', $url, $match ) ) {
674 if( wpfval( $match, 1 ) ) $rtrimed_url = rtrim( $match[1], '/\\' );
675 if( wpfval( $match, 2 ) ) $url_append_vars = '?' . trim( $match[2], '?&/\\' );
676 if( $rtrimed_url ) {
677 $home_url = rtrim( preg_replace( '#/?\?.*$#isu', '', home_url() ), '/\\' );
678 if( $rtrimed_url == $home_url ) {
679 $url = $rtrimed_url . '/';
680 } else {
681 $url = $rtrimed_url . ( wpforo_ram_get( [ $this, 'can_use_trailing_slashes' ] ) ? '/' : '' );
682 }
683 }
684 }
685
686 return $url . $url_append_vars;
687 }
688
689 public function strip_url_paged_var( $url ) {
690 $patterns = [
691 '#/' . preg_quote( wpforo_settings_get_slug( 'paged' ) ) . '/?\d*/?#isu',
692 '#[&?]wpfpaged=\d*#iu',
693 ];
694 $url = preg_replace( $patterns, '', $url );
695
696 return $this->user_trailingslashit( $url );
697 }
698
699 public function statistic_cache_clean() {
700 $this->db->query( "DELETE FROM `" . $this->db->options . "` WHERE `option_name` REGEXP '^" . wpforo_prefix( 'stat_[0-9]+' ) . "'" );
701 }
702
703 public function statistic( $mode = 'get', $template = 'all' ) {
704 $key = 'stat_' . $this->current_user_groupid;
705 if( $mode === 'get' ) {
706 if( $cached_stat = wpforo_get_option( $key, [], false ) ) {
707 $cached_stat['online_members_count'] = $this->member->online_members_count();
708 if( wpfkey( $cached_stat, 'forums' ) && wpfkey( $cached_stat, 'topics' ) && wpfkey( $cached_stat, 'posts' ) ) {
709 return wpforo_array_args_cast_and_merge( $cached_stat, $this->default->stats );
710 }
711 }
712 }
713
714 if( $mode === 'get' || $template === 'all' ) {
715 $stats['forums'] = $this->forum->get_count( [ 'is_cat' => 0 ] );
716 $stats['topics'] = $this->topic->get_count();
717 $stats['posts'] = $this->post->get_count();
718 $member_status = [ 'p.`status`' => $this->member->get_inlist_enabled_statuses() ];
719 $stats['members'] = $this->member->get_count( $member_status );
720 $stats['online_members_count'] = $this->member->online_members_count();
721 $row_count = apply_filters( 'wpforo_get_statistic_row_count', 20 );
722
723 $posts = $this->topic->get_topics( [ 'orderby' => 'modified', 'order' => 'DESC', 'row_count' => $row_count, 'private' => 0, 'status' => 0, 'permgroup' => 4 ] );
724 $first = key( $posts );
725 if( isset( $posts[ $first ] ) && ! empty( $posts[ $first ] ) && $this->perm->forum_can( 'vf', $posts[ $first ]['forumid'] ) ) {
726 $stats['last_post_title'] = $posts[ $first ]['title'];
727 $stats['last_post_url'] = $this->post->get_post_url( $posts[ $first ]['last_post'] );
728 }
729
730 $members = $this->member->get_members( [ 'orderby' => 'userid', 'status' => [ 'active' ], 'order' => 'DESC', 'row_count' => 1, 'groupids' => $this->usergroup->get_visible_usergroup_ids() ] );
731 if( isset( $members[0] ) && ! empty( $members[0] ) ) {
732 $members[0]['profile_url'] = $this->member->get_profile_url( $members[0] );
733 $stats['newest_member'] = $members[0];
734 $stats['newest_member_dname'] = wpforo_user_dname( $members[0] );
735 $stats['newest_member_profile_url'] = $members[0]['profile_url'];
736 }
737 } else {
738 $stats = wpforo_get_option( $key, $this->default->stats, false );
739 switch( $template ) {
740 case 'forum':
741 $stats['forums'] = $this->forum->get_count( [ 'is_cat' => 0 ] );
742 break;
743 case 'topic':
744 $stats['topics'] = $this->topic->get_count();
745 $posts = $this->topic->get_topics( [ 'orderby' => 'modified', 'order' => 'DESC', 'row_count' => 1 ] );
746 if( isset( $posts[0] ) && ! empty( $posts[0] ) && $this->perm->forum_can( 'vf', $posts[0]['forumid'] ) ) {
747 $stats['last_post_title'] = $posts[0]['title'];
748 $stats['last_post_url'] = $this->post->get_post_url( $posts[0]['last_post'] );
749 }
750 break;
751 case 'post':
752 $stats['posts'] = $this->post->get_count();
753 $posts = $this->topic->get_topics( [ 'orderby' => 'modified', 'order' => 'DESC', 'row_count' => 1 ] );
754 if( isset( $posts[0] ) && ! empty( $posts[0] ) && $this->perm->forum_can( 'vf', $posts[0]['forumid'] ) ) {
755 $stats['last_post_title'] = $posts[0]['title'];
756 $stats['last_post_url'] = $this->post->get_post_url( $posts[0]['last_post'] );
757 }
758 break;
759 case 'user':
760 $member_status = [ 'p.`status`' => $this->member->get_inlist_enabled_statuses() ];
761 $stats['members'] = $this->member->get_count( $member_status );
762 $stats['online_members_count'] = $this->member->online_members_count();
763
764 $members = $this->member->get_members( [ 'orderby' => 'userid', 'order' => 'DESC', 'row_count' => 1, 'groupids' => $this->usergroup->get_visible_usergroup_ids() ] );
765 if( isset( $members[0] ) && ! empty( $members[0] ) ) {
766 $members[0]['profile_url'] = $this->member->get_profile_url( $members[0] );
767 $stats['newest_member'] = $members[0];
768 $stats['newest_member_dname'] = wpforo_user_dname( $members[0] );
769 $stats['newest_member_profile_url'] = $members[0]['profile_url'];
770 }
771 break;
772 }
773 }
774
775 $stats = apply_filters( 'wpforo_get_statistic_array_filter', $stats );
776 $stats = wpforo_array_args_cast_and_merge( $stats, $this->default->stats );
777 wpforo_update_option( $key, $stats );
778
779 return $stats;
780 }
781
782 public function init_current_object() {
783 $this->reset_current_object();
784 $this->current_object['items_per_page'] = $this->post->get_option_items_per_page();
785 $url = $this->current_url;
786 $get = $this->GET;
787
788 if( ! is_wpforo_page( $url ) ) return;
789
790 $current_url = wpforo_get_url_query_vars_str( $url );
791
792 $current_object = [];
793 if( wpfkey( $get, 'wpfs' ) || wpfval( $get, 'foro' ) === 'search' ) $this->current_object['template'] = 'search';
794 if( wpfval( $get, 'wpforo' ) || wpfval( $get, 'foro' ) ) {
795 $request = ( wpfval( $get, 'wpforo' ) ) ? wpfval( $get, 'wpforo' ) : wpfval( $get, 'foro' );
796 if( $request === 'page' ) $this->current_object['template'] = 'page';
797 }
798
799 $template_key = '';
800 $wpf_url = preg_replace( '#/?\?.*$#isu', '', $current_url );
801 $wpf_url_parse = array_values( array_filter( explode( '/', trim( $wpf_url, '/' ) ) ) );
802 if( array_key_exists( 0, $wpf_url_parse ) && in_array( $wpf_url_parse[0], $this->board->routes ) ) {
803 $this->current_object['route'] = $wpf_url_parse[0];
804 unset( $wpf_url_parse[0] );
805
806 if( in_array( $this->current_object['route'], $this->board->base_routes, true ) ) {
807 $this->current_object['template'] = ( $template_key = wpforo_settings_get_slug_key( $this->current_object['route'] ) ) !== 'member' ? $template_key : '';
808
809 if( $this->current_object['template'] === 'register' ) {
810 $this->form->current['template'] = 'register';
811 $this->form->current['value']['user_login'] = sanitize_user( (string) wpfval( $_POST, 'wpfreg', 'user_login' ) );
812 $this->form->current['value']['user_email'] = sanitize_email( (string) wpfval( $_POST, 'wpfreg', 'user_email' ) );
813 $this->form->current['varname'] = 'wpfreg';
814 } elseif( $this->current_object['template'] === 'logout' && $this->current_userid ) {
815 $this->action->logout();
816 }
817 }
818
819 if( $this->current_userid && in_array( $this->current_object['template'], [ 'register', 'login', 'lostpassword' ], true ) ) wpforo_redirect_to();
820 }
821 $wpf_url_parse = array_reverse( $wpf_url_parse );
822
823 if( in_array( wpforo_settings_get_slug( 'paged' ), $wpf_url_parse ) ) {
824 foreach( $wpf_url_parse as $key => $value ) {
825 if( $value === wpforo_settings_get_slug( 'paged' ) ) {
826 unset( $wpf_url_parse[ $key ] );
827 break;
828 }
829 if( is_numeric( $value ) ) $paged = intval( $value );
830
831 unset( $wpf_url_parse[ $key ] );
832 }
833 }
834 if( $_paged = intval( wpfval( $get, 'wpfpaged' ) ) ) $paged = $_paged;
835 $current_object['paged'] = ( isset( $paged ) && $paged > 0 ) ? $paged : 1;
836 $current_object['orderby'] = wpfval( $get, 'orderby' );
837
838 $wpf_url_parse = array_values( $wpf_url_parse );
839
840 if( ! $this->current_object['template'] ) {
841 $current_object = apply_filters( 'wpforo_before_init_current_object', $current_object, $wpf_url_parse );
842 if( wpfkey( $current_object, 'template' ) ) $this->current_object['template'] = (string) wpfval( $current_object, 'template' );
843 }
844
845 if( ! $this->current_object['template'] ) {
846 $templates = $template_key === 'member' ? $this->tpl->get_member_templates_list() : $this->tpl->get_templates_list();
847 if( $templates ) {
848 if( $template_key === 'member' ) {
849 if( count( $wpf_url_parse ) > 1 ) {
850 $nickname = end( $wpf_url_parse );
851 $profile_route = prev( $wpf_url_parse );
852 array_pop( $wpf_url_parse );
853 array_pop( $wpf_url_parse );
854 array_push( $wpf_url_parse, $nickname, $profile_route );
855 } else {
856 if( ! in_array( end( $wpf_url_parse ), $templates ) ) $wpf_url_parse[] = wpforo_settings_get_slug( 'profile' );
857 }
858 }
859 $__slug = end( $wpf_url_parse );
860 foreach( $templates as $template ) {
861 if( $__slug === wpforo_settings_get_slug( $template ) ) {
862 $this->current_object['template'] = $template;
863 $current_object['qvars'] = $wpf_url_parse;
864 array_pop( $current_object['qvars'] );
865 $current_object['qvars'] = array_reverse( $current_object['qvars'] );
866 break;
867 }
868 }
869
870 if( $template_key === 'member' ) {
871 if( ! $this->current_object['template'] && ! $wpf_url_parse ) {
872 if( $this->current_userid ) {
873 $this->current_object['template'] = 'profile';
874 } else {
875 wp_safe_redirect( wpforo_login_url() );
876 exit();
877 }
878 } elseif( ! wpforo_is_member_template( $this->current_object['template'] ) ) {
879 $this->current_object['template'] = '';
880 $current_object['qvars'] = [];
881 }
882 }
883 }
884 }
885
886 if( ! $this->current_object['template'] ) {
887 $this->current_object['template'] = 'forum';
888 $this->form->current['varname'] = 'thread';
889 if( isset( $wpf_url_parse[0] ) ) {
890 if( isset( $wpf_url_parse[1] ) ) {
891 $current_object['topic_slug'] = $wpf_url_parse[0];
892 $current_object['forum_slug'] = $wpf_url_parse[1];
893 $this->current_object['template'] = 'post';
894 $this->form->current['varname'] = 'post';
895 } else {
896 $current_object['forum_slug'] = $wpf_url_parse[0];
897 $this->current_object['template'] = 'topic';
898 $this->form->current['varname'] = 'thread';
899 }
900 }
901 }
902
903 $current_object = apply_filters( 'wpforo_after_init_current_template', $current_object, $wpf_url_parse, $get );
904 if( wpfkey( $current_object, 'template' ) ) $this->current_object['template'] = (string) wpfval( $current_object, 'template' );
905
906 if( $this->current_object['template'] ) {
907 if( wpforo_is_member_template( $this->current_object['template'] ) ) {
908 if( ! wpfval( $current_object, 'userid' ) && ! wpfval( $current_object, 'user_nicename' ) ) {
909 if( $qvar0 = wpfval( $current_object['qvars'], 0 ) ) {
910 if( wpforo_setting( 'profiles', 'url_structure' ) === 'id' ) {
911 $current_object['userid'] = $qvar0;
912 } else {
913 $current_object['user_nicename'] = $qvar0;
914 }
915 } else {
916 if( $this->current_userid ) {
917 $current_object['userid'] = $this->current_userid;
918 } else {
919 wp_safe_redirect( wpforo_login_url() );
920 exit();
921 }
922 }
923 }
924
925 // redirect old type of member urls to new one
926 if( $template_key !== 'member' ) {
927 if( count( $current_object['qvars'] ) === 1 ) {
928 $redirect = wpforo_url(
929 array_shift( $current_object['qvars'] ) . ( $this->current_object['template'] !== 'profile' ? '/' . wpforo_settings_get_slug( $this->current_object['template'] ) : '' ),
930 'member'
931 );
932 } else {
933 $redirect = wpforo_url(
934 array_shift( $current_object['qvars'] ) . '/' . wpforo_settings_get_slug( $this->current_object['template'] ) . '/' . implode( '/', $current_object['qvars'] ),
935 'member'
936 );
937 }
938 wp_safe_redirect( $redirect );
939 exit();
940 }
941 // redirectiong
942
943 } elseif( $this->current_object['template'] === 'search' ) {
944 $args = [
945 'needle' => sanitize_text_field( wpfval( $get, 'wpfs' ) ),
946 'type' => sanitize_text_field( wpfval( $get, 'wpfin' ) ),
947 'date_period' => intval( wpfval( $get, 'wpfd' ) ),
948 'forumids' => (array) wpfval( $get, 'wpff' ),
949 'offset' => ( $current_object['paged'] - 1 ) * $this->current_object['items_per_page'],
950 'row_count' => $this->current_object['items_per_page'],
951 'orderby' => 'relevancy',
952 'order' => 'desc',
953 'postids' => [],
954 ];
955 if( ! empty( $get['wpfob'] ) ) {
956 $args['orderby'] = sanitize_text_field( $get['wpfob'] );
957 } elseif( in_array( wpfval( $args, 'type' ), [ 'tag', 'user-posts', 'user-topics' ], true ) ) {
958 $args['orderby'] = 'date';
959 }
960 $wpfo = strtolower( wpfval( $get, 'wpfo' ) );
961 if( in_array( $wpfo, [ 'asc', 'desc' ], true ) ) $args['order'] = $wpfo;
962 $sdata = array_filter( (array) wpfval( $get, 'data' ) );
963 $args['postids'] = $this->postmeta->search( $sdata );
964 $current_object['args'] = $args;
965 if( $sdata && ! $args['postids'] ) {
966 $current_object['items_count'] = 0;
967 $current_object['posts'] = [];
968 } else {
969 $current_object['posts'] = $this->post->search( $args, $current_object['items_count'] );
970 }
971 } elseif( $this->current_object['template'] === 'recent' ) {
972 $current_object['items_per_page'] = wpforo_setting( 'topics', 'topics_per_page' );
973 } elseif( $this->current_object['template'] === 'tags' ) {
974 $current_object['items_per_page'] = wpforo_setting( 'tags', 'per_page' );
975 $args = [
976 'offset' => ( $current_object['paged'] - 1 ) * $current_object['items_per_page'],
977 'row_count' => $current_object['items_per_page'],
978 ];
979 $current_object['tags'] = $this->topic->get_tags( $args, $current_object['items_count'] );
980 } elseif( $this->current_object['template'] === 'members' ) {
981 $current_object['items_per_page'] = wpforo_setting( 'members', 'members_per_page' );
982
983 $this->form->current['template'] = 'members';
984 $this->form->current['value'] = $get;
985 $this->form->current['varname'] = '';
986
987 if( ! empty( $get['_wpfms'] ) ) {
988 $users_include = [];
989 $search_fields_names = $this->member->get_search_fields_names();
990
991 $wpfms = ( isset( $get['wpfms'] ) ) ? sanitize_text_field( $get['wpfms'] ) : '';
992 if( $wpfms ) {
993 $users_include = $this->member->search( $wpfms, $search_fields_names );
994 } else {
995 if( $filters = array_filter( $get, function( $v ) { return ! ( is_null( $v ) || $v === false || $v === '' ); } ) ) {
996 $filters = array_merge( array_filter( (array) wpfval( $get, 'data' ), function( $v ) { return ! ( is_null( $v ) || $v === false || $v === '' ); } ), $filters );
997 unset( $filters['data'] );
998 $args = [];
999 foreach( $filters as $filter_key => $filter ) {
1000 if( in_array( $filter_key, $search_fields_names ) ) {
1001 $args[ $filter_key ] = $filter;
1002 }
1003 }
1004 $users_include = $this->member->filter( $args );
1005 }
1006 }
1007
1008 $users_include = apply_filters( 'wpforo_member_search_users_include', $users_include );
1009 }
1010 $member_status = $this->member->get_inlist_enabled_statuses();
1011 $args = [
1012 'offset' => ( $current_object['paged'] - 1 ) * $current_object['items_per_page'],
1013 'row_count' => $current_object['items_per_page'],
1014 'status' => $member_status,
1015 'groupids' => $this->usergroup->get_visible_usergroup_ids(),
1016 ];
1017 switch( wpforo_setting( 'members', 'list_order' ) ) {
1018 case 'online_time':
1019 $args['orderby'] = 'online_time';
1020 $args['order'] = 'desc';
1021 break;
1022 case 'posts__asc':
1023 $args['orderby'] = 'posts';
1024 $args['order'] = 'ASC';
1025 break;
1026 case 'user_registered__asc':
1027 $args['orderby'] = 'user_registered';
1028 $args['order'] = 'ASC';
1029 break;
1030 case 'user_registered__desc':
1031 $args['orderby'] = 'user_registered';
1032 $args['order'] = 'DESC';
1033 break;
1034 case 'display_name__asc':
1035 $args['orderby'] = 'display_name';
1036 $args['order'] = 'ASC';
1037 break;
1038 case 'display_name__desc':
1039 $args['orderby'] = 'display_name';
1040 $args['order'] = 'DESC';
1041 break;
1042 default:
1043 $args['orderby'] = 'posts';
1044 $args['order'] = 'DESC';
1045 break;
1046 }
1047 if( ! empty( $users_include ) ) $args['include'] = $users_include;
1048 $current_object['members'] = $this->member->get_members( $args, $current_object['items_count'] );
1049 if( isset( $users_include ) && empty( $users_include ) ) {
1050 $current_object['members'] = [];
1051 $current_object['items_count'] = 0;
1052 }
1053 } elseif( $this->current_object['template'] === 'add-topic' ) {
1054 if( $qvar0 = (int) wpfval( $current_object['qvars'], 0 ) ) {
1055 $forum = (array) $this->forum->get_forum( $qvar0 );
1056 if( ! $forum || (int) wpfval( $forum, 'is_cat' ) ) {
1057 wp_safe_redirect( wpforo_home_url( wpforo_settings_get_slug( 'add-topic' ) ), 301 );
1058 exit();
1059 }
1060 }
1061 }
1062 }
1063
1064 if( wpfval( $current_object, 'userid' ) || wpfval( $current_object, 'user_nicename' ) ) {
1065 $args = [];
1066 if( isset( $current_object['userid'] ) ) $args['userid'] = $current_object['userid'];
1067 if( isset( $current_object['user_nicename'] ) ) $args['user_nicename'] = $current_object['user_nicename'];
1068 $selected_user = $this->member->get_member( $args );
1069 if( isset( $current_object['userid'] ) && empty( $selected_user ) ) $selected_user = $this->member->get_member( [ 'user_nicename' => $current_object['userid'] ] );
1070 if( $selected_user ) {
1071 $current_object['user'] = $selected_user;
1072 $current_object['userid'] = $selected_user['userid'];
1073 $current_object['user_nicename'] = $selected_user['user_nicename'];
1074 $current_object['user_is_same_current_user'] = ! empty( $this->current_userid ) && $selected_user['userid'] === $this->current_userid;
1075
1076 if( $this->tpl->can_view_template( $this->current_object['template'], $current_object['user'] ) ) {
1077 switch( $this->current_object['template'] ) {
1078 case 'activity':
1079 $args = [
1080 'offset' => ( $current_object['paged'] - 1 ) * $this->current_object['items_per_page'],
1081 'row_count' => $this->current_object['items_per_page'],
1082 'userid' => $current_object['userid'],
1083 'orderby' => '`created` DESC, `postid` DESC',
1084 'check_private' => true,
1085 'is_first_post' => wpfval( $this->GET, 'filter' ) ? $this->GET['filter'] === 'topics' : null,
1086 ];
1087 $current_object['items_count'] = 0;
1088 $current_object['activities'] = $this->post->get_posts( $args, $current_object['items_count'] );
1089 break;
1090 case 'favored':
1091 $current_object['filter'] = strtolower( wpfval( WPF()->GET, 'filter' ) );
1092 if( ! in_array( $current_object['filter'], [ 'likes', 'dislikes' ], true ) ) $current_object['filter'] = 'bookmarks';
1093 if( $current_object['filter'] === 'likes' ) {
1094 $postids = $this->reaction->get_reactions_col(
1095 'postid', [
1096 'userid' => $current_object['userid'],
1097 'type_include' => 'up',
1098 ]
1099 );
1100 } elseif( $current_object['filter'] === 'dislikes' ) {
1101 $postids = $this->reaction->get_reactions_col(
1102 'postid', [
1103 'userid' => $current_object['userid'],
1104 'type_include' => 'down',
1105 ]
1106 );
1107 } else {
1108 $postids = $this->bookmark->get_bookmarks_col(
1109 'postid', [
1110 'userid' => $current_object['userid'],
1111 'boardid' => $this->board->get_current( 'boardid' ),
1112 'status' => true,
1113 ]
1114 );
1115 }
1116
1117 if( $postids ) {
1118 $args = [
1119 'offset' => ( $current_object['paged'] - 1 ) * $this->current_object['items_per_page'],
1120 'row_count' => $this->current_object['items_per_page'],
1121 'orderby' => '`created` DESC, `postid` DESC',
1122 'check_private' => true,
1123 'include' => $postids,
1124 ];
1125 $current_object['items_count'] = 0;
1126 $current_object['favored_posts'] = $this->post->get_posts( $args, $current_object['items_count'] );
1127 } else {
1128 $current_object['items_count'] = 0;
1129 $current_object['favored_posts'] = [];
1130 }
1131 break;
1132 case 'account':
1133 $this->form->current['template'] = 'account';
1134 $this->form->current['varname'] = 'member';
1135 $this->form->current['value'] = array_merge( $current_object['user'], (array) wpfval( $_POST, 'member' ) );
1136 break;
1137 default:
1138 $this->form->current['template'] = 'profile';
1139 $this->form->current['value'] = $current_object['user'];
1140 break;
1141 }
1142 } else {
1143 if( ! $this->current_userid ) {
1144 wp_safe_redirect( wpforo_login_url() );
1145 exit();
1146 }
1147 $current_object['is_404'] = true;
1148 }
1149
1150 } else {
1151 $current_object['is_404'] = true;
1152 }
1153 }
1154
1155 if( wpfval( $current_object, 'topic_slug' ) ) {
1156 $topic = $this->topic->get_topic( [ 'slug' => $current_object['topic_slug'] ], false );
1157 if( ! empty( $topic ) ) {
1158 $topic_forumid = intval( wpfval( $topic, 'forumid' ) );
1159 $is_owner = wpforo_is_owner( $topic['userid'], $topic['email'] );
1160
1161 if( apply_filters( 'wpforo_current_object_topic_protect', true, $topic ) && $topic_forumid && ( ! $this->perm->forum_can( 'vf', $topic_forumid ) || ( ! $is_owner && ! $this->perm->forum_can( 'vt', $topic_forumid ) ) || ( wpfval( $topic, 'private' ) && ! $is_owner && ! $this->perm->forum_can( 'vp', $topic_forumid ) ) || ( wpfval( $topic, 'status' ) && ! $is_owner && ! $this->perm->forum_can( 'au', $topic_forumid ) ) ) ) {
1162 if( ! $this->current_userid ) {
1163 wp_safe_redirect( wpforo_login_url() );
1164 exit();
1165 }
1166 $current_object['is_404'] = true;
1167 } else {
1168 $current_object['topic'] = $topic;
1169 $current_object['topicid'] = $topic['topicid'];
1170 $current_object['og_text'] = (string) wpfval( $topic, 'title' );
1171 }
1172 } else {
1173 $current_object['is_404'] = true;
1174 }
1175 }
1176
1177 if( wpfval( $current_object, 'forum_slug' ) ) {
1178 $args = ( empty( $topic ) ? [ 'slug' => $current_object['forum_slug'] ] : $topic['forumid'] );
1179 if( $forum = $this->forum->get_forum( $args ) ) {
1180 if( ! empty( $topic ) && strtolower( $current_object['forum_slug'] ) !== strtolower( $forum['slug'] ) ) {
1181 wp_safe_redirect( $this->topic->get_topic_url( $topic, $forum ), 301 );
1182 exit();
1183 }
1184 if( $forum['is_cat'] ) $this->current_object['template'] = 'forum';
1185 $current_object['forum'] = $forum;
1186 $current_object['forumid'] = $forum['forumid'];
1187 $current_object['forum_desc'] = $forum['description'];
1188 $current_object['forum_meta_key'] = $forum['meta_key'];
1189 $current_object['forum_meta_desc'] = $forum['meta_desc'];
1190 $current_object['og_text'] = $forum['title'];
1191 $current_object['layout'] = $this->forum->get_layout( $forum );
1192
1193 if( $this->current_object['template'] === 'topic' ) {
1194 $current_object['items_per_page'] = wpforo_setting( 'topics', 'topics_per_page' );
1195 $args = [
1196 'offset' => ( $current_object['paged'] - 1 ) * $current_object['items_per_page'],
1197 'row_count' => $current_object['items_per_page'],
1198 'forumid' => $current_object['forumid'],
1199 'orderby' => 'type, modified',
1200 'order' => 'DESC',
1201 ];
1202 $args = apply_filters( 'wpforo_topic_list_args', $args );
1203 $current_object['topics'] = $this->topic->get_topics( $args, $current_object['items_count'] );
1204 }
1205 } else {
1206 $current_object['is_404'] = true;
1207 }
1208 }
1209
1210 if( in_array( $this->current_object['template'], [ 'forum', 'topic' ] ) ) {
1211 if( ! empty( $forum ) ) {
1212 $current_object['categories'] = [ $forum ];
1213 } else {
1214 $current_object['categories'] = $this->forum->get_forums( [ "type" => 'category' ] );
1215 }
1216 }
1217
1218 if( $this->current_object['template'] === 'post' && ! empty( $forum ) && ! empty( $topic ) ) {
1219 $current_object['items_per_page'] = $this->post->get_option_items_per_page( $current_object['layout'] );
1220
1221 $args = [
1222 'forumid' => $forum['forumid'],
1223 'topicid' => $topic['topicid'],
1224 'offset' => ( $current_object['paged'] - 1 ) * $current_object['items_per_page'],
1225 'row_count' => $current_object['items_per_page'],
1226 ];
1227 if( $current_object['layout'] == 4 ) {
1228 $args['parentid'] = 0;
1229 } elseif( $current_object['layout'] == 3 ) {
1230 $args['parentid'] = 0;
1231 switch( $current_object['orderby'] ) {
1232 case 'oldest':
1233 $args['orderby'] = '`is_first_post` DESC, `is_answer` DESC, `created` ASC, `postid` ASC';
1234 break;
1235 case 'newest':
1236 $args['orderby'] = '`is_first_post` DESC, `is_answer` DESC, `modified` DESC, `postid` DESC';
1237 break;
1238 default:
1239 $args['orderby'] = '`is_first_post` DESC, `is_answer` DESC, `votes` DESC, `created` ASC, `postid` ASC';
1240 break;
1241 }
1242 }
1243 if( $this->post->get_option_union_first_post( $current_object['layout'] ) ) $args['union_first_post'] = true;
1244 $args = apply_filters( 'wpforo_post_list_args', $args );
1245 $current_object['posts'] = $this->post->get_posts( $args, $current_object['items_count'] );
1246 }
1247
1248 $this->current_object = wpforo_parse_args( $current_object, $this->current_object );
1249
1250 $this->current_object = apply_filters( 'wpforo_after_init_current_object', $this->current_object, $wpf_url_parse );
1251
1252 if( $this->current_object['template'] ) {
1253 /**
1254 * redirect not logged-in users to login page when that user no access to this page
1255 */
1256 if( ! $this->current_userid && $this->current_object['forumid'] && ( ( in_array( $this->current_object['template'], [ 'forum', 'topic' ] ) && ! $this->perm->forum_can( 'vf', $this->current_object['forumid'] ) ) || ( $this->current_object['template'] === 'post' && ! $this->perm->forum_can( 'vt', $this->current_object['forumid'] ) ) ) ) {
1257 wp_safe_redirect( wpforo_login_url() );
1258 exit();
1259 }
1260
1261 if( $this->current_object['template'] === 'cantlogin' ) {
1262 if( $this->current_userid ) {
1263 WPF()->ram_cache->set( 'USER_LOGIN_REFERER', WPF()->current_user_login );
1264 wp_logout();
1265 } else {
1266 wp_safe_redirect( wpforo_login_url() );
1267 exit();
1268 }
1269 }
1270
1271 /**
1272 * redirect to the first page when paged var is greater items_count
1273 */
1274 if( $this->current_object['items_count'] && $this->current_object['paged'] > 1 && ( ( $this->current_object['paged'] - 1 ) * $this->current_object['items_per_page'] ) >= $this->current_object['items_count'] ) {
1275 wp_safe_redirect( $this->strip_url_paged_var( $this->current_url ), 301 );
1276 exit();
1277 }
1278 } else {
1279 $this->current_object['is_404'] = true;
1280 }
1281 }
1282
1283 public function is_installed() {
1284 return WPFORO_VERSION === wpforo_get_option( 'version', null, false );
1285 }
1286
1287 public function can_use_this_slug( $slug ) {
1288 $return = ! in_array( $slug, $this->settings->slugs, true ) && ! in_array( $slug, array_keys( $this->settings->slugs ), true );
1289
1290 return apply_filters( 'wpforo_can_use_this_slug', $return, $slug );
1291 }
1292 }
1293