PluginProbe
wpForo Forum / 2.2.9
wpForo Forum v2.2.9
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.9, at wpforo.php

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