PluginProbe
wpForo Forum / 2.0.8
wpForo Forum v2.0.8
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.8, at wpforo.php

1,305 lines 50.3 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.8
9 * Text Domain: wpforo
10 * Domain Path: /languages
11 */
12
13 namespace wpforo;
14
15 define( 'WPFORO_VERSION', '2.0.8' );
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|bmp|webp|svg|tiff)/?(?:\?[^/]*)?$#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
416 // In many cases people leave the website URL protocol as http, but website works with https
417 // In this case colors.css and phrases.js are not loaded because of http blocking by browser
418 $upload_dir['baseurl'] = is_ssl() ? str_replace( 'http://', 'https://', $upload_dir['baseurl'] ) : $upload_dir['baseurl'];
419
420 $this->folders['wp_upload'] = [
421 'dir' => $this->fix_dir_sep( $upload_dir['basedir'] ),
422 'url' => $this->fix_url_sep( $upload_dir['baseurl'] ),
423 ];
424 $this->folders['wp_upload']['url//'] = preg_replace( '#^https?:#i', '', $this->folders['wp_upload']['url'] );
425 $this->folders['upload'] = [
426 'dir' => $this->folders['wp_upload']['dir'] . DIRECTORY_SEPARATOR . "wpforo" . ( $boardid ? '_' . $boardid : '' ),
427 'url' => $this->folders['wp_upload']['url'] . "/wpforo" . ( $boardid ? '_' . $boardid : '' ),
428 'url//' => $this->folders['wp_upload']['url//'] . "/wpforo" . ( $boardid ? '_' . $boardid : '' ),
429 ];
430 foreach( $this->_folders as $folder ) {
431 $this->folders[ $folder ] = [
432 'dir' => $this->folders['upload']['dir'] . DIRECTORY_SEPARATOR . trim( $this->fix_dir_sep( $folder ), DIRECTORY_SEPARATOR ),
433 'url' => $this->folders['upload']['url'] . "/" . trim( $this->fix_url_sep( $folder ), '/' ),
434 'url//' => $this->folders['upload']['url//'] . "/" . trim( $this->fix_url_sep( $folder ), '/' ),
435 ];
436 }
437 do_action( 'wpforo_after_init_folders', $this->folders );
438 }
439
440 private function requires() {
441 require_once WPFORO_DIR . '/includes/functions.php';
442 require_once WPFORO_DIR . '/includes/functions-template.php';
443 require_once WPFORO_DIR . '/includes/hooks.php';
444 require_once WPFORO_DIR . '/includes/installation.php';
445 require_once WPFORO_DIR . '/integrations/functions.php';
446 if( wpforo_is_admin() ) require_once WPFORO_DIR . '/admin/index.php';
447 }
448
449 public function admin_init() {
450 if( wpforo_is_admin() ) {
451 $this->change_board();
452
453 $this->check_issues();
454
455 if( strpos( wpforo_get_request_uri(), 'user-new.php' ) === false ) {
456 if( ! $this->member->get_groupid( $this->current_userid ) ) {
457 $this->member->synchronize_user( $this->current_userid );
458 }
459 }
460
461 if( ! $this->usergroup->can_manage_forum() && wpforo_current_user_is( 'admin' ) ) {
462 $this->member->set_groupid( $this->current_userid, 1 );
463 }
464 }
465 }
466
467 public function check_issues() {
468 //Make sure all users profiles are created
469 if( $this->is_installed() ) {
470 if( apply_filters( 'wpforo_profile_synchronization_notice', true ) ) {
471 if( is_multisite() ) {
472 $sql = "SELECT COUNT(`user_id`) FROM `" . WPF()->db->usermeta . "` WHERE `meta_key` LIKE '" . WPF()->blog_prefix . "capabilities' AND `user_id` NOT IN( SELECT `userid` FROM `" . WPF()->tables->profiles . "` )";
473 } else {
474 $sql = "SELECT COUNT(`ID`) as user_id FROM `" . WPF()->db->users . "` WHERE `ID` NOT IN( SELECT `userid` FROM `" . WPF()->tables->profiles . "` )";
475 }
476 if( (int) $this->db->get_var( $sql ) ) add_action( 'admin_notices', 'wpforo_profile_notice', 10 );
477 }
478 }
479 //Make sure tables structures are correct for current version
480 $wpforo_version_db = wpforo_get_option( 'version_db', null, false );
481 if( ! $wpforo_version_db || version_compare( $wpforo_version_db, WPFORO_VERSION, '<' ) || wpforo_setting( 'general', 'debug_mode' ) ) {
482 if( 'tables' !== wpfval( $_GET, 'view' ) ) {
483 $db_note = false;
484 $problems = wpforo_database_check();
485 if( ! empty( $problems ) ) {
486 foreach( $problems as $key => $problem ) {
487 if( wpfval( $problem, 'fields' ) ) $db_note = true;
488 if( wpfval( $problem, 'exists' ) ) $db_note = true;
489 if( $key === 'data' ) $db_note = true;
490 }
491 if( $db_note ) {
492 add_action( 'admin_notices', 'wpforo_database_notice', 10 );
493 }
494 } else {
495 wpforo_update_option( 'version_db', WPFORO_VERSION );
496 }
497 }
498 }
499 //Check cache plugins
500 $not_excluded_plugins = WPF()->cache->cache_plugins_status();
501 if( ! empty( $not_excluded_plugins ) ) {
502 add_action( 'admin_notices', 'wpforo_cache_information', 10 );
503 }
504 }
505
506 public function init() {
507 do_action( 'wpforo_before_init' );
508 $this->init_classes();
509 $this->init_current_url();
510 do_action( 'wpforo_core_inited' );
511 $this->init_current_object();
512 do_action( 'wpforo_after_init' );
513 }
514
515 public function shortcode_atts_to_url( $atts ) {
516 $url = wpforo_home_url();
517
518 $args = shortcode_atts(
519 [
520 'item' => 'forum',
521 'id' => 0,
522 'slug' => '',
523 ],
524 (array) $atts
525 );
526
527 if( $args['item'] === 'profile' && ! $args['id'] ) $args['id'] = $this->current_userid;
528
529 if( $args['item'] === 'add-topic' ) {
530 $forum = $this->forum->get_forum( ( $args['slug'] ?: $args['id'] ) );
531 $forumid = (int) wpfval( $forum, 'is_cat' ) ? 0 : (int) wpfval( $forum, 'forumid' );
532 $url = wpforo_home_url( wpforo_settings_get_slug( 'add-topic' ) . '/' . $forumid );
533 } elseif( $args['item'] === 'recent' ) {
534 $url = wpforo_settings_get_slug( 'recent' ) . '/';
535 if( trim( $args['slug'] ) ) $url .= '?view=' . wpforo_settings_get_slug( $args['slug'] );
536 $url = wpforo_home_url( $url );
537 } elseif( $args['id'] || $args['slug'] ) {
538 $getid = ( $args['slug'] ?: $args['id'] );
539 if( $args['item'] === 'topic' ) {
540 $url = $this->topic->get_topic_url( $getid );
541 } elseif( $args['item'] === 'profile' ) {
542 $url = $this->member->get_profile_url( $getid );
543 } else {
544 $url = $this->forum->get_forum_url( $getid );
545 }
546 } elseif( $args['item'] === 'login' ) {
547 $url = wpforo_login_url();
548 } elseif( $args['item'] === 'register' ) {
549 $url = wpforo_register_url();
550 } elseif( $args['item'] === 'lostpassword' ) {
551 $url = wpforo_lostpassword_url();
552 }
553
554 return $url;
555 }
556
557 public function init_current_url( $atts = [] ) {
558 if( is_scalar( $atts ) ) {
559 $url = $atts;
560 $atts = [];
561 } else {
562 $url = wpforo_get_request_uri();
563 }
564 if( $atts || is_wpforo_shortcode_page( $url ) ) {
565 if( $atts || ( $atts = get_wpforo_shortcode_atts( '', $url ) ) ) {
566 $url = $this->shortcode_atts_to_url( $atts );
567 } else {
568 $url = wpforo_home_url();
569 }
570 } elseif( is_wpforo_url( $url ) && preg_match( '#/' . preg_quote( wpforo_settings_get_slug( 'postid' ) ) . '/(\d+)/?$#isu', strtok( $url, '?' ), $matches ) ) {
571 $post_url = $this->post->get_post_url( $matches[1] );
572 if( $post_url !== wpforo_home_url() ) $url = $post_url;
573 }
574
575 if( function_exists( 'pll_default_language' ) ) {
576 $qvar = wpforo_get_url_query_vars_str( $url );
577 $qvar = preg_replace( '#^' . preg_quote( pll_default_language() ) . '/#', '', $qvar );
578 $url = $this->user_trailingslashit( home_url( $qvar ) );
579 }
580
581 $url = wpforo_fix_url( $url );
582 $url = preg_replace( '#\#[^/?&]*$#iu', '', $url );
583 parse_str( parse_url( $url, PHP_URL_QUERY ), $get );
584 $get = array_merge( (array) $get, $_GET );
585 $get = wp_unslash( $get );
586
587 $this->current_url = apply_filters( 'wpforo_init_current_url', $url );
588 $this->GET = apply_filters( 'wpforo_init_current_url_GET', $get );
589
590 do_action( 'wpforo_after_init_current_url', $atts );
591 }
592
593 private function init_defaults() {
594 date_default_timezone_set( 'UTC' );
595 ini_set( 'date.timezone', 'UTC' );
596
597 $this->default = new stdClass;
598
599 $this->default->current_object = [
600 'route' => '',
601 'template' => '',
602 'qvars' => [],
603 'args' => [],
604 'layout' => 1,
605 'og_text' => '',
606 'paged' => 1,
607 'items_count' => 0,
608 'items_per_page' => 15,
609 'is_404' => false,
610 'user_is_same_current_user' => false,
611 'orderby' => null,
612 'members' => [],
613 'categories' => [],
614 'topics' => [],
615 'posts' => [],
616 'user' => [],
617 'userid' => 0,
618 'user_nicename' => '',
619 'forum' => [],
620 'forumid' => 0,
621 'forum_slug' => '',
622 'forum_desc' => '',
623 'forum_meta_key' => '',
624 'forum_meta_desc' => '',
625 'topic' => [],
626 'topicid' => 0,
627 'topic_slug' => '',
628 'tags' => [],
629 'load_tinymce' => false,
630 ];
631
632 $this->default->tools_cleanup = [
633 'user_reg_days_ago' => 7,
634 'auto_cleanup_users' => 0,
635 'usergroup' => [ 1 => '0', 5 => '0', 2 => '1', 3 => '0' ],
636 ];
637
638 $this->default->tools_misc = [
639 'admin_note' => '',
640 'admin_note_groups' => [ 1, 2, 3, 4, 5 ],
641 'admin_note_pages' => [ 'forum' ],
642 ];
643
644 $this->default->stats = [
645 'forums' => 0,
646 'topics' => 0,
647 'posts' => 0,
648 'members' => 0,
649 'online_members_count' => 0,
650 'last_post_title' => '',
651 'last_post_url' => '',
652 'newest_member' => [],
653 'newest_member_dname' => '',
654 'newest_member_profile_url' => '',
655 ];
656
657 $this->default->dissmissed = [
658 'recaptcha_backend_note' => 0,
659 'recaptcha_note' => 0,
660 'addons_css_update' => 0,
661 ];
662 }
663
664 private function reset_current_object() {
665 $this->current_object = $this->default->current_object;
666 }
667
668 private function init_options() {
669 $this->tools_cleanup = wpforo_get_option( 'tools_cleanup', $this->default->tools_cleanup );
670 $this->tools_misc = wpforo_get_option( 'tools_misc', $this->default->tools_misc );
671 $this->dissmissed = wpforo_get_option( 'dissmissed', $this->default->dissmissed );
672 }
673
674 public function can_use_trailing_slashes() {
675 return '/' === substr( get_option( 'permalink_structure', '' ), - 1, 1 );
676 }
677
678 public function user_trailingslashit( $url ) {
679 $rtrimed_url = '';
680 $url_append_vars = '';
681 if( preg_match( '#^(.+?)(/?[?&].*)?$#isu', $url, $match ) ) {
682 if( wpfval( $match, 1 ) ) $rtrimed_url = rtrim( $match[1], '/\\' );
683 if( wpfval( $match, 2 ) ) $url_append_vars = '?' . trim( $match[2], '?&/\\' );
684 if( $rtrimed_url ) {
685 $home_url = rtrim( preg_replace( '#/?\?.*$#isu', '', home_url() ), '/\\' );
686 if( $rtrimed_url == $home_url ) {
687 $url = $rtrimed_url . '/';
688 } else {
689 $url = $rtrimed_url . ( wpforo_ram_get( [ $this, 'can_use_trailing_slashes' ] ) ? '/' : '' );
690 }
691 }
692 }
693
694 return $url . $url_append_vars;
695 }
696
697 public function strip_url_paged_var( $url ) {
698 $patterns = [
699 '#/' . preg_quote( wpforo_settings_get_slug( 'paged' ) ) . '/?\d*/?#isu',
700 '#[&?]wpfpaged=\d*#iu',
701 ];
702 $url = preg_replace( $patterns, '', $url );
703
704 return $this->user_trailingslashit( $url );
705 }
706
707 public function statistic_cache_clean() {
708 $this->db->query( "DELETE FROM `" . $this->db->options . "` WHERE `option_name` REGEXP '^" . wpforo_prefix( 'stat_[0-9]+' ) . "'" );
709 }
710
711 public function statistic( $mode = 'get', $template = 'all' ) {
712 $key = 'stat_' . $this->current_user_groupid;
713 if( $mode === 'get' ) {
714 if( $cached_stat = wpforo_get_option( $key, [], false ) ) {
715 $cached_stat['online_members_count'] = $this->member->online_members_count();
716 if( wpfkey( $cached_stat, 'forums' ) && wpfkey( $cached_stat, 'topics' ) && wpfkey( $cached_stat, 'posts' ) ) {
717 return wpforo_array_args_cast_and_merge( $cached_stat, $this->default->stats );
718 }
719 }
720 }
721
722 if( $mode === 'get' || $template === 'all' ) {
723 $stats['forums'] = $this->forum->get_count( [ 'is_cat' => 0 ] );
724 $stats['topics'] = $this->topic->get_count();
725 $stats['posts'] = $this->post->get_count();
726 $member_status = [ 'p.`status`' => $this->member->get_inlist_enabled_statuses() ];
727 $stats['members'] = $this->member->get_count( $member_status );
728 $stats['online_members_count'] = $this->member->online_members_count();
729 $row_count = apply_filters( 'wpforo_get_statistic_row_count', 20 );
730
731 $posts = $this->topic->get_topics( [ 'orderby' => 'modified', 'order' => 'DESC', 'row_count' => $row_count, 'private' => 0, 'status' => 0, 'permgroup' => 4 ] );
732 $first = key( $posts );
733 if( isset( $posts[ $first ] ) && ! empty( $posts[ $first ] ) && $this->perm->forum_can( 'vf', $posts[ $first ]['forumid'] ) ) {
734 $stats['last_post_title'] = $posts[ $first ]['title'];
735 $stats['last_post_url'] = $this->post->get_post_url( $posts[ $first ]['last_post'] );
736 }
737
738 $members = $this->member->get_members( [ 'orderby' => 'userid', 'status' => [ 'active' ], 'order' => 'DESC', 'row_count' => 1, 'groupids' => $this->usergroup->get_visible_usergroup_ids() ] );
739 if( isset( $members[0] ) && ! empty( $members[0] ) ) {
740 $members[0]['profile_url'] = $this->member->get_profile_url( $members[0] );
741 $stats['newest_member'] = $members[0];
742 $stats['newest_member_dname'] = wpforo_user_dname( $members[0] );
743 $stats['newest_member_profile_url'] = $members[0]['profile_url'];
744 }
745 } else {
746 $stats = wpforo_get_option( $key, $this->default->stats, false );
747 switch( $template ) {
748 case 'forum':
749 $stats['forums'] = $this->forum->get_count( [ 'is_cat' => 0 ] );
750 break;
751 case 'topic':
752 $stats['topics'] = $this->topic->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 'post':
760 $stats['posts'] = $this->post->get_count();
761 $posts = $this->topic->get_topics( [ 'orderby' => 'modified', 'order' => 'DESC', 'row_count' => 1 ] );
762 if( isset( $posts[0] ) && ! empty( $posts[0] ) && $this->perm->forum_can( 'vf', $posts[0]['forumid'] ) ) {
763 $stats['last_post_title'] = $posts[0]['title'];
764 $stats['last_post_url'] = $this->post->get_post_url( $posts[0]['last_post'] );
765 }
766 break;
767 case 'user':
768 $member_status = [ 'p.`status`' => $this->member->get_inlist_enabled_statuses() ];
769 $stats['members'] = $this->member->get_count( $member_status );
770 $stats['online_members_count'] = $this->member->online_members_count();
771
772 $members = $this->member->get_members( [ 'orderby' => 'userid', 'order' => 'DESC', 'row_count' => 1, 'groupids' => $this->usergroup->get_visible_usergroup_ids() ] );
773 if( isset( $members[0] ) && ! empty( $members[0] ) ) {
774 $members[0]['profile_url'] = $this->member->get_profile_url( $members[0] );
775 $stats['newest_member'] = $members[0];
776 $stats['newest_member_dname'] = wpforo_user_dname( $members[0] );
777 $stats['newest_member_profile_url'] = $members[0]['profile_url'];
778 }
779 break;
780 }
781 }
782
783 $stats = apply_filters( 'wpforo_get_statistic_array_filter', $stats );
784 $stats = wpforo_array_args_cast_and_merge( $stats, $this->default->stats );
785 wpforo_update_option( $key, $stats );
786
787 return $stats;
788 }
789
790 public function init_current_object() {
791 $this->reset_current_object();
792 $this->current_object['items_per_page'] = $this->post->get_option_items_per_page();
793 $url = $this->current_url;
794 $get = $this->GET;
795
796 if( ! is_wpforo_page( $url ) ) return;
797
798 $current_url = wpforo_get_url_query_vars_str( $url );
799
800 $current_object = [];
801 if( wpfkey( $get, 'wpfs' ) || wpfval( $get, 'foro' ) === 'search' ) $this->current_object['template'] = 'search';
802 if( wpfval( $get, 'wpforo' ) || wpfval( $get, 'foro' ) ) {
803 $request = ( wpfval( $get, 'wpforo' ) ) ? wpfval( $get, 'wpforo' ) : wpfval( $get, 'foro' );
804 if( $request === 'page' ) $this->current_object['template'] = 'page';
805 }
806
807 $template_key = '';
808 $wpf_url = preg_replace( '#/?\?.*$#isu', '', $current_url );
809 $wpf_url_parse = array_values( array_filter( explode( '/', trim( $wpf_url, '/' ) ) ) );
810 if( array_key_exists( 0, $wpf_url_parse ) && in_array( $wpf_url_parse[0], $this->board->routes ) ) {
811 $this->current_object['route'] = $wpf_url_parse[0];
812 unset( $wpf_url_parse[0] );
813
814 if( in_array( $this->current_object['route'], $this->board->base_routes, true ) ) {
815 $this->current_object['template'] = ( $template_key = wpforo_settings_get_slug_key( $this->current_object['route'] ) ) !== 'member' ? $template_key : '';
816
817 if( $this->current_object['template'] === 'register' ) {
818 $this->form->current['template'] = 'register';
819 $this->form->current['value']['user_login'] = sanitize_user( (string) wpfval( $_POST, 'wpfreg', 'user_login' ) );
820 $this->form->current['value']['user_email'] = sanitize_email( (string) wpfval( $_POST, 'wpfreg', 'user_email' ) );
821 $this->form->current['varname'] = 'wpfreg';
822 } elseif( $this->current_object['template'] === 'logout' && $this->current_userid ) {
823 $this->action->logout();
824 }
825 }
826
827 if( $this->current_userid && in_array( $this->current_object['template'], [ 'register', 'login', 'lostpassword' ], true ) ) wpforo_redirect_to();
828 }
829 $wpf_url_parse = array_reverse( $wpf_url_parse );
830
831 if( in_array( wpforo_settings_get_slug( 'paged' ), $wpf_url_parse ) ) {
832 foreach( $wpf_url_parse as $key => $value ) {
833 if( $value === wpforo_settings_get_slug( 'paged' ) ) {
834 unset( $wpf_url_parse[ $key ] );
835 break;
836 }
837 if( is_numeric( $value ) ) $paged = intval( $value );
838
839 unset( $wpf_url_parse[ $key ] );
840 }
841 }
842 if( $_paged = intval( wpfval( $get, 'wpfpaged' ) ) ) $paged = $_paged;
843 $current_object['paged'] = ( isset( $paged ) && $paged > 0 ) ? $paged : 1;
844 $current_object['orderby'] = wpfval( $get, 'orderby' );
845
846 $wpf_url_parse = array_values( $wpf_url_parse );
847
848 if( ! $this->current_object['template'] ) {
849 $current_object = apply_filters( 'wpforo_before_init_current_object', $current_object, $wpf_url_parse );
850 if( wpfkey( $current_object, 'template' ) ) $this->current_object['template'] = (string) wpfval( $current_object, 'template' );
851 }
852
853 if( ! $this->current_object['template'] ) {
854 $templates = $template_key === 'member' ? $this->tpl->get_member_templates_list() : $this->tpl->get_templates_list();
855 if( $templates ) {
856 if( $template_key === 'member' ) {
857 if( count( $wpf_url_parse ) > 1 ) {
858 $nickname = end( $wpf_url_parse );
859 $profile_route = prev( $wpf_url_parse );
860 array_pop( $wpf_url_parse );
861 array_pop( $wpf_url_parse );
862 array_push( $wpf_url_parse, $nickname, $profile_route );
863 } else {
864 if( ! in_array( end( $wpf_url_parse ), $templates ) ) $wpf_url_parse[] = wpforo_settings_get_slug( 'profile' );
865 }
866 }
867 $__slug = end( $wpf_url_parse );
868 foreach( $templates as $template ) {
869 if( $__slug === wpforo_settings_get_slug( $template ) ) {
870 $this->current_object['template'] = $template;
871 $current_object['qvars'] = $wpf_url_parse;
872 array_pop( $current_object['qvars'] );
873 $current_object['qvars'] = array_reverse( $current_object['qvars'] );
874 break;
875 }
876 }
877
878 if( $template_key === 'member' ) {
879 if( ! $this->current_object['template'] && ! $wpf_url_parse ) {
880 if( $this->current_userid ) {
881 $this->current_object['template'] = 'profile';
882 } else {
883 wp_safe_redirect( wpforo_login_url() );
884 exit();
885 }
886 } elseif( ! wpforo_is_member_template( $this->current_object['template'] ) ) {
887 $this->current_object['template'] = '';
888 $current_object['qvars'] = [];
889 }
890 }
891 }
892 }
893
894 if( ! $this->current_object['template'] ) {
895 $this->current_object['template'] = 'forum';
896 $this->form->current['varname'] = 'thread';
897 if( isset( $wpf_url_parse[0] ) ) {
898 if( isset( $wpf_url_parse[1] ) ) {
899 $current_object['topic_slug'] = $wpf_url_parse[0];
900 $current_object['forum_slug'] = $wpf_url_parse[1];
901 $this->current_object['template'] = 'post';
902 $this->form->current['varname'] = 'post';
903 } else {
904 $current_object['forum_slug'] = $wpf_url_parse[0];
905 $this->current_object['template'] = 'topic';
906 $this->form->current['varname'] = 'thread';
907 }
908 }
909 }
910
911 $current_object = apply_filters( 'wpforo_after_init_current_template', $current_object, $wpf_url_parse, $get );
912 if( wpfkey( $current_object, 'template' ) ) $this->current_object['template'] = (string) wpfval( $current_object, 'template' );
913
914 if( $this->current_object['template'] ) {
915 if( wpforo_is_member_template( $this->current_object['template'] ) ) {
916 if( ! wpfval( $current_object, 'userid' ) && ! wpfval( $current_object, 'user_nicename' ) ) {
917 if( $qvar0 = wpfval( $current_object['qvars'], 0 ) ) {
918 if( wpforo_setting( 'profiles', 'url_structure' ) === 'id' ) {
919 $current_object['userid'] = $qvar0;
920 } else {
921 $current_object['user_nicename'] = $qvar0;
922 }
923 } else {
924 if( $this->current_userid ) {
925 $current_object['userid'] = $this->current_userid;
926 } else {
927 wp_safe_redirect( wpforo_login_url() );
928 exit();
929 }
930 }
931 }
932
933 // redirect old type of member urls to new one
934 if( $template_key !== 'member' ) {
935 if( count( $current_object['qvars'] ) === 1 ) {
936 $redirect = wpforo_url(
937 array_shift( $current_object['qvars'] ) . ( $this->current_object['template'] !== 'profile' ? '/' . wpforo_settings_get_slug( $this->current_object['template'] ) : '' ),
938 'member'
939 );
940 } else {
941 $redirect = wpforo_url(
942 array_shift( $current_object['qvars'] ) . '/' . wpforo_settings_get_slug( $this->current_object['template'] ) . '/' . implode( '/', $current_object['qvars'] ),
943 'member'
944 );
945 }
946 wp_safe_redirect( $redirect );
947 exit();
948 }
949 // redirectiong
950
951 } elseif( $this->current_object['template'] === 'search' ) {
952 $args = [
953 'needle' => sanitize_text_field( wpfval( $get, 'wpfs' ) ),
954 'type' => sanitize_text_field( wpfval( $get, 'wpfin' ) ),
955 'date_period' => intval( wpfval( $get, 'wpfd' ) ),
956 'forumids' => (array) wpfval( $get, 'wpff' ),
957 'offset' => ( $current_object['paged'] - 1 ) * $this->current_object['items_per_page'],
958 'row_count' => $this->current_object['items_per_page'],
959 'orderby' => 'relevancy',
960 'order' => 'desc',
961 'postids' => [],
962 ];
963 if( ! empty( $get['wpfob'] ) ) {
964 $args['orderby'] = sanitize_text_field( $get['wpfob'] );
965 } elseif( in_array( wpfval( $args, 'type' ), [ 'tag', 'user-posts', 'user-topics' ], true ) ) {
966 $args['orderby'] = 'date';
967 }
968 $wpfo = strtolower( wpfval( $get, 'wpfo' ) );
969 if( in_array( $wpfo, [ 'asc', 'desc' ], true ) ) $args['order'] = $wpfo;
970 $sdata = array_filter( (array) wpfval( $get, 'data' ) );
971 $args['postids'] = $this->postmeta->search( $sdata );
972 $current_object['args'] = $args;
973 if( $sdata && ! $args['postids'] ) {
974 $current_object['items_count'] = 0;
975 $current_object['posts'] = [];
976 } else {
977 $current_object['posts'] = $this->post->search( $args, $current_object['items_count'] );
978 }
979 } elseif( $this->current_object['template'] === 'recent' ) {
980 $current_object['items_per_page'] = wpforo_setting( 'topics', 'topics_per_page' );
981 } elseif( $this->current_object['template'] === 'tags' ) {
982 $current_object['items_per_page'] = wpforo_setting( 'tags', 'per_page' );
983 $args = [
984 'offset' => ( $current_object['paged'] - 1 ) * $current_object['items_per_page'],
985 'row_count' => $current_object['items_per_page'],
986 ];
987 $current_object['tags'] = $this->topic->get_tags( $args, $current_object['items_count'] );
988 } elseif( $this->current_object['template'] === 'members' ) {
989 $current_object['items_per_page'] = wpforo_setting( 'members', 'members_per_page' );
990
991 $this->form->current['template'] = 'members';
992 $this->form->current['value'] = $get;
993 $this->form->current['varname'] = '';
994
995 if( ! empty( $get['_wpfms'] ) ) {
996 $users_include = [];
997 $search_fields_names = $this->member->get_search_fields_names();
998
999 $wpfms = ( isset( $get['wpfms'] ) ) ? sanitize_text_field( $get['wpfms'] ) : '';
1000 if( $wpfms ) {
1001 $users_include = $this->member->search( $wpfms, $search_fields_names );
1002 } else {
1003 if( $filters = array_filter( $get, function( $v ) { return ! ( is_null( $v ) || $v === false || $v === '' ); } ) ) {
1004 $filters = array_merge( array_filter( (array) wpfval( $get, 'data' ), function( $v ) { return ! ( is_null( $v ) || $v === false || $v === '' ); } ), $filters );
1005 unset( $filters['data'] );
1006 $args = [];
1007 foreach( $filters as $filter_key => $filter ) {
1008 if( in_array( $filter_key, $search_fields_names ) ) {
1009 $args[ $filter_key ] = $filter;
1010 }
1011 }
1012 $users_include = $this->member->filter( $args );
1013 }
1014 }
1015
1016 $users_include = apply_filters( 'wpforo_member_search_users_include', $users_include );
1017 }
1018 $member_status = $this->member->get_inlist_enabled_statuses();
1019 $args = [
1020 'offset' => ( $current_object['paged'] - 1 ) * $current_object['items_per_page'],
1021 'row_count' => $current_object['items_per_page'],
1022 'status' => $member_status,
1023 'groupids' => $this->usergroup->get_visible_usergroup_ids(),
1024 ];
1025 switch( wpforo_setting( 'members', 'list_order' ) ) {
1026 case 'online_time':
1027 $args['orderby'] = 'online_time';
1028 $args['order'] = 'desc';
1029 break;
1030 case 'posts__asc':
1031 $args['orderby'] = 'posts';
1032 $args['order'] = 'ASC';
1033 break;
1034 case 'user_registered__asc':
1035 $args['orderby'] = 'user_registered';
1036 $args['order'] = 'ASC';
1037 break;
1038 case 'user_registered__desc':
1039 $args['orderby'] = 'user_registered';
1040 $args['order'] = 'DESC';
1041 break;
1042 case 'display_name__asc':
1043 $args['orderby'] = 'display_name';
1044 $args['order'] = 'ASC';
1045 break;
1046 case 'display_name__desc':
1047 $args['orderby'] = 'display_name';
1048 $args['order'] = 'DESC';
1049 break;
1050 default:
1051 $args['orderby'] = 'posts';
1052 $args['order'] = 'DESC';
1053 break;
1054 }
1055 if( ! empty( $users_include ) ) $args['include'] = $users_include;
1056 $current_object['members'] = $this->member->get_members( $args, $current_object['items_count'] );
1057 if( isset( $users_include ) && empty( $users_include ) ) {
1058 $current_object['members'] = [];
1059 $current_object['items_count'] = 0;
1060 }
1061 } elseif( $this->current_object['template'] === 'add-topic' ) {
1062 if( $qvar0 = (int) wpfval( $current_object['qvars'], 0 ) ) {
1063 $forum = (array) $this->forum->get_forum( $qvar0 );
1064 if( ! $forum || (int) wpfval( $forum, 'is_cat' ) ) {
1065 wp_safe_redirect( wpforo_home_url( wpforo_settings_get_slug( 'add-topic' ) ), 301 );
1066 exit();
1067 }
1068 }
1069 }
1070 }
1071
1072 if( wpfval( $current_object, 'userid' ) || wpfval( $current_object, 'user_nicename' ) ) {
1073 $args = [];
1074 if( isset( $current_object['userid'] ) ) $args['userid'] = $current_object['userid'];
1075 if( isset( $current_object['user_nicename'] ) ) $args['user_nicename'] = $current_object['user_nicename'];
1076 $selected_user = $this->member->get_member( $args );
1077 if( isset( $current_object['userid'] ) && empty( $selected_user ) ) $selected_user = $this->member->get_member( [ 'user_nicename' => $current_object['userid'] ] );
1078 if( $selected_user ) {
1079 $current_object['user'] = $selected_user;
1080 $current_object['userid'] = $selected_user['userid'];
1081 $current_object['user_nicename'] = $selected_user['user_nicename'];
1082 $current_object['user_is_same_current_user'] = ! empty( $this->current_userid ) && $selected_user['userid'] === $this->current_userid;
1083
1084 if( $this->tpl->can_view_template( $this->current_object['template'], $current_object['user'] ) ) {
1085 switch( $this->current_object['template'] ) {
1086 case 'activity':
1087 $args = [
1088 'offset' => ( $current_object['paged'] - 1 ) * $this->current_object['items_per_page'],
1089 'row_count' => $this->current_object['items_per_page'],
1090 'userid' => $current_object['userid'],
1091 'orderby' => '`created` DESC, `postid` DESC',
1092 'check_private' => true,
1093 'is_first_post' => wpfval( $this->GET, 'filter' ) ? $this->GET['filter'] === 'topics' : null,
1094 ];
1095 $current_object['items_count'] = 0;
1096 $current_object['activities'] = $this->post->get_posts( $args, $current_object['items_count'] );
1097 break;
1098 case 'favored':
1099 $current_object['filter'] = strtolower( wpfval( WPF()->GET, 'filter' ) );
1100 if( ! in_array( $current_object['filter'], [ 'likes', 'dislikes' ], true ) ) $current_object['filter'] = 'bookmarks';
1101 if( $current_object['filter'] === 'likes' ) {
1102 $postids = $this->reaction->get_reactions_col(
1103 'postid', [
1104 'userid' => $current_object['userid'],
1105 'type_include' => 'up',
1106 ]
1107 );
1108 } elseif( $current_object['filter'] === 'dislikes' ) {
1109 $postids = $this->reaction->get_reactions_col(
1110 'postid', [
1111 'userid' => $current_object['userid'],
1112 'type_include' => 'down',
1113 ]
1114 );
1115 } else {
1116 $postids = $this->bookmark->get_bookmarks_col(
1117 'postid', [
1118 'userid' => $current_object['userid'],
1119 'boardid' => $this->board->get_current( 'boardid' ),
1120 'status' => true,
1121 ]
1122 );
1123 }
1124
1125 if( $postids ) {
1126 $args = [
1127 'offset' => ( $current_object['paged'] - 1 ) * $this->current_object['items_per_page'],
1128 'row_count' => $this->current_object['items_per_page'],
1129 'orderby' => '`created` DESC, `postid` DESC',
1130 'check_private' => true,
1131 'include' => $postids,
1132 ];
1133 $current_object['items_count'] = 0;
1134 $current_object['favored_posts'] = $this->post->get_posts( $args, $current_object['items_count'] );
1135 } else {
1136 $current_object['items_count'] = 0;
1137 $current_object['favored_posts'] = [];
1138 }
1139 break;
1140 case 'account':
1141 $this->form->current['template'] = 'account';
1142 $this->form->current['varname'] = 'member';
1143 $this->form->current['value'] = array_merge( $current_object['user'], (array) wpfval( $_POST, 'member' ) );
1144 break;
1145 default:
1146 $this->form->current['template'] = 'profile';
1147 $this->form->current['value'] = $current_object['user'];
1148 break;
1149 }
1150 } else {
1151 if( ! $this->current_userid ) {
1152 wp_safe_redirect( wpforo_login_url() );
1153 exit();
1154 }
1155 $current_object['is_404'] = true;
1156 }
1157
1158 } else {
1159 $current_object['is_404'] = true;
1160 }
1161 }
1162
1163 if( wpfval( $current_object, 'topic_slug' ) ) {
1164 $topic = $this->topic->get_topic( [ 'slug' => $current_object['topic_slug'] ], false );
1165 if( ! empty( $topic ) ) {
1166 $topic_forumid = intval( wpfval( $topic, 'forumid' ) );
1167 $is_owner = wpforo_is_owner( $topic['userid'], $topic['email'] );
1168
1169 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 ) ) ) ) {
1170 if( ! $this->current_userid ) {
1171 wp_safe_redirect( wpforo_login_url() );
1172 exit();
1173 }
1174 $current_object['is_404'] = true;
1175 } else {
1176 $current_object['topic'] = $topic;
1177 $current_object['topicid'] = $topic['topicid'];
1178 $current_object['og_text'] = (string) wpfval( $topic, 'title' );
1179 }
1180 } else {
1181 $current_object['is_404'] = true;
1182 }
1183 }
1184
1185 if( wpfval( $current_object, 'forum_slug' ) ) {
1186 $args = ( empty( $topic ) ? [ 'slug' => $current_object['forum_slug'] ] : $topic['forumid'] );
1187 if( $forum = $this->forum->get_forum( $args ) ) {
1188 if( ! empty( $topic ) && strtolower( $current_object['forum_slug'] ) !== strtolower( $forum['slug'] ) ) {
1189 wp_safe_redirect( $this->topic->get_topic_url( $topic, $forum ), 301 );
1190 exit();
1191 }
1192 if( $forum['is_cat'] ) $this->current_object['template'] = 'forum';
1193 $current_object['forum'] = $forum;
1194 $current_object['forumid'] = $forum['forumid'];
1195 $current_object['forum_desc'] = $forum['description'];
1196 $current_object['forum_meta_key'] = $forum['meta_key'];
1197 $current_object['forum_meta_desc'] = $forum['meta_desc'];
1198 $current_object['og_text'] = $forum['title'];
1199 $current_object['layout'] = $this->forum->get_layout( $forum );
1200
1201 if( $this->current_object['template'] === 'topic' ) {
1202 $current_object['items_per_page'] = wpforo_setting( 'topics', 'topics_per_page' );
1203 $args = [
1204 'offset' => ( $current_object['paged'] - 1 ) * $current_object['items_per_page'],
1205 'row_count' => $current_object['items_per_page'],
1206 'forumid' => $current_object['forumid'],
1207 'orderby' => 'type, modified',
1208 'order' => 'DESC',
1209 ];
1210 $args = apply_filters( 'wpforo_topic_list_args', $args );
1211 $current_object['topics'] = $this->topic->get_topics( $args, $current_object['items_count'] );
1212 }
1213 } else {
1214 $current_object['is_404'] = true;
1215 }
1216 }
1217
1218 if( in_array( $this->current_object['template'], [ 'forum', 'topic' ] ) ) {
1219 if( ! empty( $forum ) ) {
1220 $current_object['categories'] = [ $forum ];
1221 } else {
1222 $current_object['categories'] = $this->forum->get_forums( [ "type" => 'category' ] );
1223 }
1224 }
1225
1226 if( $this->current_object['template'] === 'post' && ! empty( $forum ) && ! empty( $topic ) ) {
1227 $current_object['items_per_page'] = $this->post->get_option_items_per_page( $current_object['layout'] );
1228
1229 $args = [
1230 'forumid' => $forum['forumid'],
1231 'topicid' => $topic['topicid'],
1232 'offset' => ( $current_object['paged'] - 1 ) * $current_object['items_per_page'],
1233 'row_count' => $current_object['items_per_page'],
1234 ];
1235 if( $current_object['layout'] == 4 ) {
1236 $args['parentid'] = 0;
1237 } elseif( $current_object['layout'] == 3 ) {
1238 $args['parentid'] = 0;
1239 switch( $current_object['orderby'] ) {
1240 case 'oldest':
1241 $args['orderby'] = '`is_first_post` DESC, `is_answer` DESC, `created` ASC, `postid` ASC';
1242 break;
1243 case 'newest':
1244 $args['orderby'] = '`is_first_post` DESC, `is_answer` DESC, `modified` DESC, `postid` DESC';
1245 break;
1246 default:
1247 $args['orderby'] = '`is_first_post` DESC, `is_answer` DESC, `votes` DESC, `created` ASC, `postid` ASC';
1248 break;
1249 }
1250 }
1251 if( $this->post->get_option_union_first_post( $current_object['layout'] ) ) $args['union_first_post'] = true;
1252 $args = apply_filters( 'wpforo_post_list_args', $args );
1253 $current_object['posts'] = $this->post->get_posts( $args, $current_object['items_count'] );
1254 }
1255
1256 $this->current_object = wpforo_parse_args( $current_object, $this->current_object );
1257
1258 $this->current_object = apply_filters( 'wpforo_after_init_current_object', $this->current_object, $wpf_url_parse );
1259
1260 if( $this->current_object['template'] ) {
1261 /**
1262 * redirect not logged-in users to login page when that user no access to this page
1263 */
1264 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'] ) ) ) ) {
1265 wp_safe_redirect( wpforo_login_url() );
1266 exit();
1267 }
1268
1269 if( $this->current_object['template'] === 'cantlogin' ) {
1270 if( $this->current_userid ) {
1271 if( $this->current_user['status'] !== 'active' ){
1272 WPF()->ram_cache->set( 'USER_LOGIN_REFERER', WPF()->current_user_login );
1273 wp_logout();
1274 }else{
1275 wp_safe_redirect( wpforo_home_url() );
1276 }
1277 } else {
1278 wp_safe_redirect( wpforo_login_url() );
1279 exit();
1280 }
1281 }
1282
1283 /**
1284 * redirect to the first page when paged var is greater items_count
1285 */
1286 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'] ) {
1287 wp_safe_redirect( $this->strip_url_paged_var( $this->current_url ), 301 );
1288 exit();
1289 }
1290 } else {
1291 $this->current_object['is_404'] = true;
1292 }
1293 }
1294
1295 public function is_installed() {
1296 return WPFORO_VERSION === wpforo_get_option( 'version', null, false );
1297 }
1298
1299 public function can_use_this_slug( $slug ) {
1300 $return = ! in_array( $slug, $this->settings->slugs, true ) && ! in_array( $slug, array_keys( $this->settings->slugs ), true );
1301
1302 return apply_filters( 'wpforo_can_use_this_slug', $return, $slug );
1303 }
1304 }
1305