PluginProbe
wpForo Forum / 2.0.1
wpForo Forum v2.0.1
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.1, at wpforo.php

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