PluginProbe
wpForo Forum / 2.2.2
wpForo Forum v2.2.2
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.2.2, at wpforo.php

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