PluginProbe
wpForo Forum / 2.3.1
wpForo Forum v2.3.1
3.1.5 3.1.4 3.1.2 3.1.1 3.1.0 3.0.9 3.0.8 3.0.7 trunk 1.0.0 1.0.1 1.0.2 1.1.0 1.1.1 1.1.2 1.2.0 1.3.0 1.3.1 1.4.0 1.4.1 1.4.10 1.4.11 1.4.12 1.4.13 1.4.2 All 137 releases
wpforo / wpforo.php

wpforo.php in wpForo Forum 2.3.1, at wpforo.php

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