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

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