| 1 |
<?php |
| 2 |
/** |
| 3 |
* UsersWP Page related functions |
| 4 |
* |
| 5 |
* All UsersWP page related functions can be found here. |
| 6 |
* |
| 7 |
* @since 1.0.0 |
| 8 |
* @author GeoDirectory Team <info@wpgeodirectory.com> |
| 9 |
*/ |
| 10 |
class UsersWP_Pages { |
| 11 |
|
| 12 |
/** |
| 13 |
* Checks whether the current page is of given page type or not. |
| 14 |
* |
| 15 |
* @since 1.0.0 |
| 16 |
* @package userswp |
| 17 |
* @param string|bool $type Page type. |
| 18 |
* @return bool |
| 19 |
*/ |
| 20 |
public function is_page($type = false) { |
| 21 |
if (is_page()) { |
| 22 |
global $post; |
| 23 |
$current_page_id = isset($post->ID) ? absint($post->ID) : ''; |
| 24 |
if($current_page_id){ |
| 25 |
if ($type) { |
| 26 |
$uwp_page = uwp_get_page_id($type, false); |
| 27 |
if ( $uwp_page && ((int) $uwp_page == $current_page_id ) ) { |
| 28 |
return true; |
| 29 |
} else { |
| 30 |
return false; |
| 31 |
} |
| 32 |
} else { |
| 33 |
if ($this->is_register_page() || |
| 34 |
$this->is_login_page() || |
| 35 |
$this->is_forgot_page() || |
| 36 |
$this->is_change_page() || |
| 37 |
$this->is_reset_page() || |
| 38 |
$this->is_account_page() || |
| 39 |
$this->is_profile_page() || |
| 40 |
$this->is_users_page() || |
| 41 |
$this->is_user_item_page()) { |
| 42 |
return true; |
| 43 |
} else { |
| 44 |
return false; |
| 45 |
} |
| 46 |
} |
| 47 |
} |
| 48 |
} else { |
| 49 |
return false; |
| 50 |
} |
| 51 |
|
| 52 |
return false; |
| 53 |
} |
| 54 |
|
| 55 |
/** |
| 56 |
* Checks whether the current page is register page or not. |
| 57 |
* |
| 58 |
* @since 1.0.0 |
| 59 |
* @package userswp |
| 60 |
* @return bool |
| 61 |
*/ |
| 62 |
public function is_register_page() { |
| 63 |
return $this->is_page('register_page'); |
| 64 |
} |
| 65 |
|
| 66 |
/** |
| 67 |
* Checks whether the current page is login page or not. |
| 68 |
* |
| 69 |
* @since 1.0.0 |
| 70 |
* @package userswp |
| 71 |
* @return bool |
| 72 |
*/ |
| 73 |
public function is_login_page() { |
| 74 |
return $this->is_page('login_page'); |
| 75 |
} |
| 76 |
|
| 77 |
/** |
| 78 |
* Checks whether the current page is forgot password page or not. |
| 79 |
* |
| 80 |
* @since 1.0.0 |
| 81 |
* @package userswp |
| 82 |
* @return bool |
| 83 |
*/ |
| 84 |
public function is_forgot_page() { |
| 85 |
return $this->is_page('forgot_page'); |
| 86 |
} |
| 87 |
|
| 88 |
/** |
| 89 |
* Checks whether the current page is change password page or not. |
| 90 |
* |
| 91 |
* @since 1.0.0 |
| 92 |
* @package userswp |
| 93 |
* @return bool |
| 94 |
*/ |
| 95 |
public function is_change_page() { |
| 96 |
return $this->is_page('change_page'); |
| 97 |
} |
| 98 |
|
| 99 |
/** |
| 100 |
* Checks whether the current page is reset password page or not. |
| 101 |
* |
| 102 |
* @since 1.0.0 |
| 103 |
* @package userswp |
| 104 |
* @return bool |
| 105 |
*/ |
| 106 |
public function is_reset_page() { |
| 107 |
return $this->is_page('reset_page'); |
| 108 |
} |
| 109 |
|
| 110 |
/** |
| 111 |
* Checks whether the current page is account page or not. |
| 112 |
* |
| 113 |
* @since 1.0.0 |
| 114 |
* @package userswp |
| 115 |
* @return bool |
| 116 |
*/ |
| 117 |
public function is_account_page() { |
| 118 |
return $this->is_page('account_page'); |
| 119 |
} |
| 120 |
|
| 121 |
/** |
| 122 |
* Checks whether the current page is profile page or not. |
| 123 |
* |
| 124 |
* @since 1.0.0 |
| 125 |
* @package userswp |
| 126 |
* @return bool |
| 127 |
*/ |
| 128 |
public function is_profile_page() { |
| 129 |
return $this->is_page('profile_page'); |
| 130 |
} |
| 131 |
|
| 132 |
/** |
| 133 |
* Checks whether the current page is users page or not. |
| 134 |
* |
| 135 |
* @since 1.0.0 |
| 136 |
* @package userswp |
| 137 |
* @return bool |
| 138 |
*/ |
| 139 |
public function is_users_page() { |
| 140 |
return $this->is_page('users_page'); |
| 141 |
} |
| 142 |
|
| 143 |
/** |
| 144 |
* Checks whether the current page is users page or not. |
| 145 |
* |
| 146 |
* @since 1.1.2 |
| 147 |
* @package userswp |
| 148 |
* @return bool |
| 149 |
*/ |
| 150 |
public function is_user_item_page() { |
| 151 |
return $this->is_page('user_list_item_page'); |
| 152 |
} |
| 153 |
|
| 154 |
/** |
| 155 |
* Checks whether the current page is logged in user profile page or not. |
| 156 |
* |
| 157 |
* @since 1.0.0 |
| 158 |
* @package userswp |
| 159 |
* @return bool |
| 160 |
*/ |
| 161 |
public function is_current_user_profile_page() { |
| 162 |
if (is_user_logged_in() && |
| 163 |
$this->is_profile_page() |
| 164 |
) { |
| 165 |
$author_slug = get_query_var('uwp_profile'); |
| 166 |
if ($author_slug) { |
| 167 |
$url_type = apply_filters('uwp_profile_url_type', 'slug'); |
| 168 |
if ($url_type == 'id') { |
| 169 |
$user = get_user_by('id', $author_slug); |
| 170 |
} else { |
| 171 |
$user = get_user_by('slug', $author_slug); |
| 172 |
} |
| 173 |
|
| 174 |
if ($user && $user->ID == get_current_user_id()) { |
| 175 |
return true; |
| 176 |
} else { |
| 177 |
return false; |
| 178 |
} |
| 179 |
} else { |
| 180 |
return false; |
| 181 |
} |
| 182 |
} else { |
| 183 |
return false; |
| 184 |
} |
| 185 |
} |
| 186 |
|
| 187 |
/** |
| 188 |
* Returns all available pages as array to use in select dropdown. |
| 189 |
* |
| 190 |
* @since 1.0.0 |
| 191 |
* @package userswp |
| 192 |
* @return array Page array. |
| 193 |
*/ |
| 194 |
public function get_pages() { |
| 195 |
$pages_options = array( '' => __( 'Select a Page', 'userswp' ) ); // Blank option |
| 196 |
|
| 197 |
$pages = get_pages(); |
| 198 |
if ( $pages ) { |
| 199 |
foreach ( $pages as $page ) { |
| 200 |
$pages_options[ $page->ID ] = $page->post_title; |
| 201 |
} |
| 202 |
} |
| 203 |
return $pages_options; |
| 204 |
} |
| 205 |
|
| 206 |
/** |
| 207 |
* Gets the page slug using the given page type. |
| 208 |
* |
| 209 |
* @since 1.0.0 |
| 210 |
* @package userswp |
| 211 |
* @param string $page_type Page type. |
| 212 |
* @return string Page slug. |
| 213 |
*/ |
| 214 |
public function get_page_slug($page_type = 'register_page') { |
| 215 |
$page_id = uwp_get_page_id($page_type, 0); |
| 216 |
if ($page_id) { |
| 217 |
$slug = get_post_field( 'post_name', get_post($page_id) ); |
| 218 |
} else { |
| 219 |
$slug = false; |
| 220 |
} |
| 221 |
return $slug; |
| 222 |
|
| 223 |
} |
| 224 |
|
| 225 |
/** |
| 226 |
* Creates UsersWP page if not exists. |
| 227 |
* |
| 228 |
* @since 1.0.0 |
| 229 |
* @package userswp |
| 230 |
* @param string $slug Page slug. |
| 231 |
* @param string $option Page setting key. |
| 232 |
* @param string $page_title The post title. Default empty. |
| 233 |
* @param mixed $page_content The post content. Default empty. |
| 234 |
* @param int $post_parent Set this for the post it belongs to, if any. Default 0. |
| 235 |
* @param string $status The post status. Default 'draft'. |
| 236 |
* |
| 237 |
* @return int Page ID |
| 238 |
*/ |
| 239 |
public function create_page($slug, $option, $page_title = '', $page_content = '', $post_parent = 0, $status = 'publish') { |
| 240 |
global $wpdb, $current_user; |
| 241 |
|
| 242 |
$settings = get_option( 'uwp_settings', array()); |
| 243 |
if (isset($settings[$option])) { |
| 244 |
$option_value = $settings[$option]; |
| 245 |
} else { |
| 246 |
$option_value = false; |
| 247 |
} |
| 248 |
|
| 249 |
if ($option_value > 0 && ( $page_object = get_post( $option_value ) )) { |
| 250 |
if ('page' === $page_object->post_type && !in_array($page_object->post_status, array('pending', 'trash', 'future', 'auto-draft'))) { |
| 251 |
// Valid page is already in place |
| 252 |
return $page_object->ID; |
| 253 |
} |
| 254 |
} |
| 255 |
|
| 256 |
if(!empty($post_parent)){ |
| 257 |
$page = get_page_by_path($post_parent); |
| 258 |
if ($page) { |
| 259 |
$post_parent = $page->ID; |
| 260 |
} else { |
| 261 |
$post_parent = ''; |
| 262 |
} |
| 263 |
} |
| 264 |
|
| 265 |
if ( strlen( $page_content ) > 0 ) { |
| 266 |
// Search for an existing page with the specified page content (typically a shortcode) |
| 267 |
$valid_page_found = $wpdb->get_var( $wpdb->prepare( "SELECT ID FROM $wpdb->posts WHERE post_type='page' AND post_status NOT IN ( 'pending', 'trash', 'future', 'auto-draft' ) AND post_content LIKE %s LIMIT 1;", "%{$page_content}%" ) ); |
| 268 |
$trashed_page_found = $wpdb->get_var( $wpdb->prepare( "SELECT ID FROM $wpdb->posts WHERE post_type='page' AND post_status = 'trash' AND post_content LIKE %s LIMIT 1;", "%{$page_content}%" ) ); |
| 269 |
} else { |
| 270 |
// Search for an existing page with the specified page slug |
| 271 |
$valid_page_found = $wpdb->get_var( $wpdb->prepare( "SELECT ID FROM $wpdb->posts WHERE post_type='page' AND post_status NOT IN ( 'pending', 'trash', 'future', 'auto-draft' ) AND post_name = %s LIMIT 1;", $slug ) ); |
| 272 |
$trashed_page_found = $wpdb->get_var( $wpdb->prepare( "SELECT ID FROM $wpdb->posts WHERE post_type='page' AND post_status = 'trash' AND post_name = %s LIMIT 1;", $slug ) ); |
| 273 |
} |
| 274 |
|
| 275 |
$valid_page_found = apply_filters( 'uwp_create_page_id', $valid_page_found, $slug, $page_content ); |
| 276 |
|
| 277 |
if ( $valid_page_found ) { |
| 278 |
if ( $option ) { |
| 279 |
$settings[$option] = $valid_page_found; |
| 280 |
update_option( 'uwp_settings', $settings ); |
| 281 |
} |
| 282 |
return $valid_page_found; |
| 283 |
} |
| 284 |
|
| 285 |
if ( $trashed_page_found ) { |
| 286 |
$page_id = $trashed_page_found; |
| 287 |
$page_data = array( |
| 288 |
'ID' => $page_id, |
| 289 |
'post_status' => 'publish', |
| 290 |
'post_parent' => $post_parent, |
| 291 |
); |
| 292 |
wp_update_post( $page_data ); |
| 293 |
} else { |
| 294 |
$page_data = array( |
| 295 |
'post_status' => 'publish', |
| 296 |
'post_type' => 'page', |
| 297 |
'post_author' => $current_user->ID, |
| 298 |
'post_name' => $slug, |
| 299 |
'post_title' => $page_title, |
| 300 |
'post_content' => $page_content, |
| 301 |
'post_parent' => $post_parent, |
| 302 |
'comment_status' => 'closed', |
| 303 |
); |
| 304 |
$page_id = wp_insert_post( $page_data ); |
| 305 |
} |
| 306 |
|
| 307 |
if ( $option ) { |
| 308 |
$settings[$option] = $page_id; |
| 309 |
update_option('uwp_settings', $settings); |
| 310 |
} |
| 311 |
|
| 312 |
return $page_id; |
| 313 |
} |
| 314 |
|
| 315 |
/** |
| 316 |
* Generates default UsersWP pages. Usually called during plugin activation. |
| 317 |
* |
| 318 |
* @since 1.0.0 |
| 319 |
* @package userswp |
| 320 |
* @return void |
| 321 |
*/ |
| 322 |
public function generate_default_pages() { |
| 323 |
|
| 324 |
$this->create_page(esc_sql(_x('register', 'page_slug', 'userswp')), 'register_page', __('Register', 'userswp'), '[uwp_register]'); |
| 325 |
$this->create_page(esc_sql(_x('login', 'page_slug', 'userswp')), 'login_page', __('Login', 'userswp'), '[uwp_login]'); |
| 326 |
$this->create_page(esc_sql(_x('account', 'page_slug', 'userswp')), 'account_page', __('Account', 'userswp'), '[uwp_account]'); |
| 327 |
$this->create_page(esc_sql(_x('forgot', 'page_slug', 'userswp')), 'forgot_page', __('Forgot Password?', 'userswp'), '[uwp_forgot]'); |
| 328 |
$this->create_page(esc_sql(_x('reset', 'page_slug', 'userswp')), 'reset_page', __('Reset Password', 'userswp'), '[uwp_reset]'); |
| 329 |
$this->create_page(esc_sql(_x('change', 'page_slug', 'userswp')), 'change_page', __('Change Password', 'userswp'), '[uwp_change]'); |
| 330 |
$this->create_page(esc_sql(_x('profile', 'page_slug', 'userswp')), 'profile_page', __('Profile', 'userswp'), '[uwp_profile]'); |
| 331 |
$this->create_page(esc_sql(_x('users', 'page_slug', 'userswp')), 'users_page', __('Users', 'userswp'), '[uwp_users]'); |
| 332 |
$this->create_page(esc_sql(_x('user-list-item', 'page_slug', 'userswp')), 'user_list_item_page', __('Users List Item', 'userswp'), '[uwp_users_item]'); |
| 333 |
|
| 334 |
} |
| 335 |
|
| 336 |
|
| 337 |
/** |
| 338 |
* Generates default UsersWP pages on new wpmu blog creation. |
| 339 |
* |
| 340 |
* @since 1.0.0 |
| 341 |
* @package userswp |
| 342 |
* |
| 343 |
* @param int $blog_id Blog ID. |
| 344 |
*/ |
| 345 |
public function wpmu_generate_default_pages_on_new_site( $blog_id ) { |
| 346 |
|
| 347 |
if (uwp_get_installation_type() != 'multi_na_all') { |
| 348 |
return; |
| 349 |
} |
| 350 |
|
| 351 |
if ( ! function_exists( 'is_plugin_active_for_network' ) ) { |
| 352 |
require_once( ABSPATH . '/wp-admin/includes/plugin.php' ); |
| 353 |
} |
| 354 |
|
| 355 |
// Bail if plugin is not network activated. |
| 356 |
if ( ! is_plugin_active_for_network( 'userswp/userswp.php' ) ) { |
| 357 |
return; |
| 358 |
} |
| 359 |
|
| 360 |
// Switch to the new blog. |
| 361 |
switch_to_blog( $blog_id ); |
| 362 |
|
| 363 |
$this->generate_default_pages(); |
| 364 |
|
| 365 |
// Restore original blog. |
| 366 |
restore_current_blog(); |
| 367 |
} |
| 368 |
|
| 369 |
/** |
| 370 |
* Gets the UsersWP page permalink based on page type. |
| 371 |
* |
| 372 |
* @since 1.0.0 |
| 373 |
* @package userswp |
| 374 |
* @param string $page Page type. |
| 375 |
* @return string Page permalink. |
| 376 |
*/ |
| 377 |
public function get_page_link($page) { |
| 378 |
$link = ""; |
| 379 |
switch ($page) { |
| 380 |
case 'register': |
| 381 |
$page_id = uwp_get_page_id('register_page', false); |
| 382 |
break; |
| 383 |
|
| 384 |
case 'login': |
| 385 |
$page_id = uwp_get_page_id('login_page', false); |
| 386 |
break; |
| 387 |
|
| 388 |
case 'forgot': |
| 389 |
$page_id = uwp_get_page_id('forgot_page', false); |
| 390 |
break; |
| 391 |
|
| 392 |
case 'account': |
| 393 |
$page_id = uwp_get_page_id('account_page', false); |
| 394 |
break; |
| 395 |
|
| 396 |
case 'profile': |
| 397 |
$page_id = uwp_get_page_id('profile_page', false); |
| 398 |
break; |
| 399 |
|
| 400 |
case 'users': |
| 401 |
$page_id = uwp_get_page_id('users_page', false); |
| 402 |
break; |
| 403 |
|
| 404 |
default: |
| 405 |
$page_id = uwp_get_page_id($page, false); |
| 406 |
break; |
| 407 |
} |
| 408 |
|
| 409 |
if ($page_id) { |
| 410 |
$link = get_permalink($page_id); |
| 411 |
} |
| 412 |
|
| 413 |
if ( ! empty( $link ) && function_exists( 'PLL' ) ) { |
| 414 |
$link = PLL()->links_model->add_language_to_link( $link, PLL()->curlang ); |
| 415 |
} |
| 416 |
|
| 417 |
return $link; |
| 418 |
} |
| 419 |
|
| 420 |
/** |
| 421 |
* Builds the profile page url based on the tab and sub tab given |
| 422 |
* yoursite.com/profile/username |
| 423 |
* yoursite.com/profile/username/tab |
| 424 |
* yoursite.com/profile/username/tab/subtab |
| 425 |
* |
| 426 |
* @since 1.0.0 |
| 427 |
* @package userswp |
| 428 |
* @param int $user_id User ID. |
| 429 |
* @param string|bool $tab Optional. Main tab |
| 430 |
* @param string|bool $subtab Optional. Sub tab. |
| 431 |
* @return string Built profile page link. |
| 432 |
*/ |
| 433 |
public function build_profile_tab_url($user_id, $tab = false, $subtab = false) { |
| 434 |
|
| 435 |
$link = apply_filters('uwp_profile_link', get_author_posts_url($user_id), $user_id); |
| 436 |
|
| 437 |
if ($link != '') { |
| 438 |
if (isset($_REQUEST['page_id'])) { |
| 439 |
$permalink_structure = 'DEFAULT'; |
| 440 |
} else { |
| 441 |
$permalink_structure = 'CUSTOM'; |
| 442 |
$link = rtrim($link, '/') . '/'; |
| 443 |
} |
| 444 |
|
| 445 |
if ('DEFAULT' == $permalink_structure) { |
| 446 |
$link = add_query_arg( |
| 447 |
array( |
| 448 |
'uwp_tab' => $tab, |
| 449 |
'uwp_subtab' => $subtab |
| 450 |
), |
| 451 |
$link |
| 452 |
); |
| 453 |
} else { |
| 454 |
if ($tab) { |
| 455 |
$link = $link . $tab; |
| 456 |
} |
| 457 |
|
| 458 |
if ($subtab) { |
| 459 |
$link = $link .'/'.$subtab; |
| 460 |
} |
| 461 |
} |
| 462 |
} |
| 463 |
|
| 464 |
return trailingslashit( $link ); |
| 465 |
} |
| 466 |
|
| 467 |
/** |
| 468 |
* To get the page ID |
| 469 |
* |
| 470 |
* @param $type |
| 471 |
* @param bool $get_link |
| 472 |
* |
| 473 |
* @return bool|false|int|NULL|string |
| 474 |
*/ |
| 475 |
public function get_page_id($type, $get_link = false) { |
| 476 |
|
| 477 |
$link = false; |
| 478 |
$page_id = uwp_get_option($type, false, false); |
| 479 |
|
| 480 |
if ($page_id && $page_id > 0) { |
| 481 |
if (uwp_is_wpml()) { |
| 482 |
$wpml_page_id = uwp_wpml_object_id($page_id, 'page', true); |
| 483 |
if (!empty($wpml_page_id)) { |
| 484 |
$page_id = $wpml_page_id; |
| 485 |
} |
| 486 |
} |
| 487 |
$link = $page_id; |
| 488 |
|
| 489 |
if($get_link){ |
| 490 |
$link = get_permalink($page_id); |
| 491 |
} |
| 492 |
} |
| 493 |
|
| 494 |
return $link; |
| 495 |
} |
| 496 |
|
| 497 |
/** |
| 498 |
* Add a post display state for special UWP pages in the page list table. |
| 499 |
* |
| 500 |
* @param array $post_states An array of post display states. |
| 501 |
* @param WP_Post $post The current post object. |
| 502 |
* |
| 503 |
* @return mixed |
| 504 |
*/ |
| 505 |
public function add_display_post_states( $post_states, $post ) { |
| 506 |
if ( uwp_get_page_id( 'register_page' ) == $post->ID ) { |
| 507 |
$post_states['uwp_register_page'] = __( 'UWP Register Page', 'userswp' ); |
| 508 |
} |
| 509 |
|
| 510 |
if ( uwp_get_page_id( 'login_page' ) == $post->ID ) { |
| 511 |
$post_states['uwp_login_page'] = __( 'UWP Login Page', 'userswp' ); |
| 512 |
} |
| 513 |
|
| 514 |
if ( uwp_get_page_id( 'forgot_page' ) == $post->ID ) { |
| 515 |
$post_states['uwp_forgot_page'] = __( 'UWP Forgot Password Page', 'userswp' ); |
| 516 |
} |
| 517 |
|
| 518 |
if ( uwp_get_page_id( 'reset_page' ) == $post->ID ) { |
| 519 |
$post_states['uwp_reset_page'] = __( 'UWP Reset Password Page', 'userswp' ); |
| 520 |
} |
| 521 |
|
| 522 |
if ( uwp_get_page_id( 'account_page' ) == $post->ID ) { |
| 523 |
$post_states['uwp_account_page'] = __( 'UWP Account Page', 'userswp' ); |
| 524 |
} |
| 525 |
|
| 526 |
if ( uwp_get_page_id( 'profile_page' ) == $post->ID ) { |
| 527 |
$post_states['uwp_profile_page'] = __( 'UWP Profile Page', 'userswp' ); |
| 528 |
} |
| 529 |
|
| 530 |
if ( uwp_get_page_id( 'users_page' ) == $post->ID ) { |
| 531 |
$post_states['uwp_users_page'] = __( 'UWP Users Page', 'userswp' ); |
| 532 |
} |
| 533 |
|
| 534 |
if ( uwp_get_page_id( 'user_list_item_page' ) == $post->ID ) { |
| 535 |
$post_states['uwp_user_list_item_page'] = __( 'UWP User Item Page', 'userswp' ); |
| 536 |
} |
| 537 |
|
| 538 |
if ( uwp_get_page_id( 'change_page' ) == $post->ID ) { |
| 539 |
$post_states['uwp_change_page'] = __( 'UWP Change Password Page', 'userswp' ); |
| 540 |
} |
| 541 |
|
| 542 |
return $post_states; |
| 543 |
} |
| 544 |
|
| 545 |
} |