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

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

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