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