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

1,368 lines 51.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /*
3 * Plugin Name: wpForo
4 * Plugin URI: https://wpforo.com
5 * Description: WordPress Forum plugin. wpForo is a full-fledged forum solution for your community. Comes with multiple modern forum layouts.
6 * Author: gVectors Team
7 * Author URI: https://gvectors.com/
8 * Version: 2.2.7
9 * Text Domain: wpforo
10 * Domain Path: /languages
11 */
12
13 namespace wpforo;
14
15 define( 'WPFORO_VERSION', '2.2.7' );
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 : (string) wpfval( $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 $users_include = $this->member->search( $wpfms, $search_fields_names );
1056 } else {
1057 if( $filters = array_filter( $get, function( $v ) { return ! ( is_null( $v ) || $v === false || $v === '' ); } ) ) {
1058 $filters = array_merge( array_filter( (array) wpfval( $get, 'data' ), function( $v ) { return ! ( is_null( $v ) || $v === false || $v === '' ); } ), $filters );
1059 unset( $filters['data'] );
1060 $args = [];
1061 foreach( $filters as $filter_key => $filter ) {
1062 if( in_array( $filter_key, $search_fields_names ) ) {
1063 $args[ $filter_key ] = $filter;
1064 }
1065 }
1066 $users_include = $this->member->filter( $args );
1067 }
1068 }
1069
1070 $users_include = apply_filters( 'wpforo_member_search_users_include', $users_include );
1071 }
1072 $member_status = $this->member->get_inlist_enabled_statuses();
1073 $args = [
1074 'offset' => ( $current_object['paged'] - 1 ) * $current_object['items_per_page'],
1075 'row_count' => $current_object['items_per_page'],
1076 'status' => $member_status,
1077 'groupids' => $this->usergroup->get_visible_usergroup_ids(),
1078 ];
1079 switch( wpforo_setting( 'members', 'list_order' ) ) {
1080 case 'online_time':
1081 $args['orderby'] = 'online_time';
1082 $args['order'] = 'desc';
1083 break;
1084 case 'posts__asc':
1085 $args['orderby'] = 'posts';
1086 $args['order'] = 'ASC';
1087 break;
1088 case 'user_registered__asc':
1089 $args['orderby'] = 'user_registered';
1090 $args['order'] = 'ASC';
1091 break;
1092 case 'user_registered__desc':
1093 $args['orderby'] = 'user_registered';
1094 $args['order'] = 'DESC';
1095 break;
1096 case 'display_name__asc':
1097 $args['orderby'] = 'display_name';
1098 $args['order'] = 'ASC';
1099 break;
1100 case 'display_name__desc':
1101 $args['orderby'] = 'display_name';
1102 $args['order'] = 'DESC';
1103 break;
1104 default:
1105 $args['orderby'] = 'posts';
1106 $args['order'] = 'DESC';
1107 break;
1108 }
1109 if( ! empty( $users_include ) ) $args['include'] = $users_include;
1110 $current_object['members'] = $this->member->get_members( $args, $current_object['items_count'] );
1111 if( isset( $users_include ) && empty( $users_include ) ) {
1112 $current_object['members'] = [];
1113 $current_object['items_count'] = 0;
1114 }
1115 } elseif( $this->current_object['template'] === 'add-topic' ) {
1116 if( $qvar0 = (int) wpfval( $current_object['qvars'], 0 ) ) {
1117 $forum = (array) $this->forum->get_forum( $qvar0 );
1118 if( ! $forum || (int) wpfval( $forum, 'is_cat' ) ) {
1119 wp_safe_redirect( wpforo_home_url( wpforo_settings_get_slug( 'add-topic' ) ), 301 );
1120 exit();
1121 }
1122 }
1123 }
1124 }
1125
1126 if( wpfval( $current_object, 'userid' ) || wpfval( $current_object, 'user_nicename' ) ) {
1127 $args = [];
1128 if( isset( $current_object['userid'] ) ) $args['userid'] = $current_object['userid'];
1129 if( isset( $current_object['user_nicename'] ) ) $args['user_nicename'] = $current_object['user_nicename'];
1130 $selected_user = $this->member->get_member( $args );
1131 if( isset( $current_object['userid'] ) && empty( $selected_user ) ) $selected_user = $this->member->get_member( [ 'user_nicename' => $current_object['userid'] ] );
1132 $selected_user = apply_filters( 'wpforo_init_current_selected_user', $selected_user );
1133 if( $selected_user ) {
1134 $current_object['user'] = $selected_user;
1135 $current_object['userid'] = $selected_user['userid'];
1136 $current_object['user_nicename'] = $selected_user['user_nicename'];
1137 $current_object['user_is_same_current_user'] = ! empty( $this->current_userid ) && $selected_user['userid'] === $this->current_userid;
1138
1139 if( $this->tpl->can_view_template( $this->current_object['template'], $current_object['user'] ) ) {
1140 switch( $this->current_object['template'] ) {
1141 case 'activity':
1142 $args = [
1143 'offset' => ( $current_object['paged'] - 1 ) * $this->current_object['items_per_page'],
1144 'row_count' => $this->current_object['items_per_page'],
1145 'userid' => $current_object['userid'],
1146 'orderby' => '`created` DESC, `postid` DESC',
1147 'check_private' => true,
1148 'is_first_post' => wpfval( $this->GET, 'filter' ) ? $this->GET['filter'] === 'topics' : null,
1149 ];
1150 $current_object['items_count'] = 0;
1151 $current_object['activities'] = $this->post->get_posts( $args, $current_object['items_count'] );
1152 break;
1153 case 'favored':
1154 $current_object['filter'] = strtolower( (string) wpfval( WPF()->GET, 'filter' ) );
1155 if( ! in_array( $current_object['filter'], [ 'likes', 'dislikes' ], true ) ) $current_object['filter'] = 'bookmarks';
1156 if( $current_object['filter'] === 'likes' ) {
1157 $postids = $this->reaction->get_reactions_col(
1158 'postid', [
1159 'userid' => $current_object['userid'],
1160 'type_include' => 'up',
1161 ]
1162 );
1163 } elseif( $current_object['filter'] === 'dislikes' ) {
1164 $postids = $this->reaction->get_reactions_col(
1165 'postid', [
1166 'userid' => $current_object['userid'],
1167 'type_include' => 'down',
1168 ]
1169 );
1170 } else {
1171 $postids = $this->bookmark->get_bookmarks_col(
1172 'postid', [
1173 'userid' => $current_object['userid'],
1174 'boardid' => $this->board->get_current( 'boardid' ),
1175 'status' => true,
1176 ]
1177 );
1178 }
1179
1180 if( $postids ) {
1181 $args = [
1182 'offset' => ( $current_object['paged'] - 1 ) * $this->current_object['items_per_page'],
1183 'row_count' => $this->current_object['items_per_page'],
1184 'orderby' => '`created` DESC, `postid` DESC',
1185 'check_private' => true,
1186 'include' => $postids,
1187 ];
1188 $current_object['items_count'] = 0;
1189 $current_object['favored_posts'] = $this->post->get_posts( $args, $current_object['items_count'] );
1190 } else {
1191 $current_object['items_count'] = 0;
1192 $current_object['favored_posts'] = [];
1193 }
1194 break;
1195 case 'account':
1196 $this->form->current['template'] = 'account';
1197 $this->form->current['varname'] = 'member';
1198 $this->form->current['value'] = array_merge( $current_object['user'], (array) wpfval( $_POST, 'member' ) );
1199 break;
1200 default:
1201 $this->form->current['template'] = 'profile';
1202 $this->form->current['value'] = $current_object['user'];
1203 break;
1204 }
1205 } else {
1206 if( ! $this->current_userid ) {
1207 wp_safe_redirect( wpforo_login_url() );
1208 exit();
1209 }
1210 $current_object['is_404'] = true;
1211 }
1212
1213 } else {
1214 $current_object['is_404'] = true;
1215 }
1216 }
1217
1218 if( wpfval( $current_object, 'topic_slug' ) ) {
1219 $topic = $this->topic->get_topic( [ 'slug' => $current_object['topic_slug'] ], false );
1220 if( ! empty( $topic ) ) {
1221 $topic_forumid = intval( wpfval( $topic, 'forumid' ) );
1222 $is_owner = wpforo_is_owner( $topic['userid'], $topic['email'] );
1223
1224 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 ) ) ) ) {
1225 if( ! $this->current_userid ) {
1226 wp_safe_redirect( wpforo_login_url() );
1227 exit();
1228 }
1229 $current_object['is_404'] = true;
1230 } else {
1231 $current_object['topic'] = $topic;
1232 $current_object['topicid'] = $topic['topicid'];
1233 $current_object['og_text'] = (string) wpfval( $topic, 'title' );
1234 }
1235 } else {
1236 $current_object['is_404'] = true;
1237 }
1238 }
1239
1240 if( wpfval( $current_object, 'forum_slug' ) ) {
1241 $args = ( empty( $topic ) ? [ 'slug' => $current_object['forum_slug'] ] : $topic['forumid'] );
1242 if( $forum = $this->forum->get_forum( $args ) ) {
1243 if( ! empty( $topic ) && strtolower( (string) $current_object['forum_slug'] ) !== strtolower( (string) $forum['slug'] ) ) {
1244 wp_safe_redirect( $this->topic->get_url( $topic, $forum ), 301 );
1245 exit();
1246 }
1247 if( $forum['is_cat'] ) $this->current_object['template'] = 'forum';
1248 $current_object['forum'] = $forum;
1249 $current_object['forumid'] = $forum['forumid'];
1250 $current_object['forum_desc'] = $forum['description'];
1251 $current_object['forum_meta_key'] = $forum['meta_key'];
1252 $current_object['forum_meta_desc'] = $forum['meta_desc'];
1253 $current_object['og_text'] = $forum['title'];
1254 $current_object['layout'] = $this->forum->get_layout( $forum );
1255
1256 if( $this->current_object['template'] === 'topic' ) {
1257 $current_object['items_per_page'] = wpforo_setting( 'topics', 'topics_per_page' );
1258 $args = [
1259 'offset' => ( $current_object['paged'] - 1 ) * $current_object['items_per_page'],
1260 'row_count' => $current_object['items_per_page'],
1261 'forumid' => $current_object['forumid'],
1262 'orderby' => 'type, modified',
1263 'order' => 'DESC',
1264 ];
1265 $args = apply_filters( 'wpforo_topic_list_args', $args );
1266 $current_object['topics'] = $this->topic->get_topics( $args, $current_object['items_count'] );
1267 }
1268 } else {
1269 $current_object['is_404'] = true;
1270 }
1271 }
1272
1273 if( in_array( $this->current_object['template'], [ 'forum', 'topic' ] ) ) {
1274 if( ! empty( $forum ) ) {
1275 $current_object['categories'] = [ $forum ];
1276 } else {
1277 $current_object['categories'] = $this->forum->get_forums( [ "type" => 'category' ] );
1278 }
1279 }
1280
1281 if( $this->current_object['template'] === 'post' && ! empty( $forum ) && ! empty( $topic ) ) {
1282 $current_object['items_per_page'] = $this->post->get_option_items_per_page( $current_object['layout'] );
1283
1284 $args = [
1285 'forumid' => $forum['forumid'],
1286 'topicid' => $topic['topicid'],
1287 'offset' => ( $current_object['paged'] - 1 ) * $current_object['items_per_page'],
1288 'row_count' => $current_object['items_per_page'],
1289 ];
1290 if( $current_object['layout'] == 4 ) {
1291 $args['parentid'] = 0;
1292 } elseif( $current_object['layout'] == 3 ) {
1293 $args['parentid'] = 0;
1294 switch( $current_object['orderby'] ) {
1295 case 'oldest':
1296 $args['orderby'] = '`is_first_post` DESC, `is_answer` DESC, `created` ASC, `postid` ASC';
1297 break;
1298 case 'newest':
1299 $args['orderby'] = '`is_first_post` DESC, `is_answer` DESC, `modified` DESC, `postid` DESC';
1300 break;
1301 default:
1302 $args['orderby'] = '`is_first_post` DESC, `is_answer` DESC, `votes` DESC, `created` ASC, `postid` ASC';
1303 break;
1304 }
1305 }
1306 if( $this->post->get_option_union_first_post( $current_object['layout'] ) ) $args['union_first_post'] = true;
1307 $args = apply_filters( 'wpforo_post_list_args', $args );
1308 $current_object['posts'] = $this->post->get_posts( $args, $current_object['items_count'] );
1309 }
1310
1311 $this->current_object = wpforo_parse_args( $current_object, $this->current_object );
1312
1313 $this->current_object = apply_filters( 'wpforo_after_init_current_object', $this->current_object, $wpf_url_parse );
1314
1315 if( $this->current_object['template'] ) {
1316 /**
1317 * redirect not logged-in users to login page when that user no access to this page
1318 */
1319 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'] ) ) ) ) {
1320 wp_safe_redirect( wpforo_login_url() );
1321 exit();
1322 }
1323
1324 if( $this->current_object['template'] === 'cantlogin' ) {
1325 if( $this->current_userid ) {
1326 if( $this->current_user['status'] !== 'active' ){
1327 WPF()->ram_cache->set( 'USER_LOGIN_REFERER', WPF()->current_user_login );
1328 wp_logout();
1329 }else{
1330 wp_safe_redirect( wpforo_home_url() );
1331 }
1332 } else {
1333 wp_safe_redirect( wpforo_login_url() );
1334 exit();
1335 }
1336 }
1337
1338 /**
1339 * redirect to the first page when paged var is greater items_count
1340 */
1341 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'] ) {
1342 wp_safe_redirect( $this->strip_url_paged_var( $this->current_url ), 301 );
1343 exit();
1344 }
1345 } else {
1346 $this->current_object['is_404'] = true;
1347 }
1348 }
1349
1350 public function get_version() {
1351 return wpforo_get_option( 'version', null, false );
1352 }
1353
1354 public function need_activation() {
1355 return WPFORO_VERSION !== $this->get_version();
1356 }
1357
1358 public function is_installed() {
1359 return (bool) $this->get_version();
1360 }
1361
1362 public function can_use_this_slug( $slug ) {
1363 $return = ! in_array( $slug, $this->settings->slugs, true ) && ! in_array( $slug, array_keys( $this->settings->slugs ), true );
1364
1365 return apply_filters( 'wpforo_can_use_this_slug', $return, $slug );
1366 }
1367 }
1368