| @@ -19,35 +19,38 @@ | ||
| 19 | 19 | */ |
| 20 | 20 | public function is_page($type = false) { |
| 21 | 21 | if (is_page()) { |
| 22 | 22 | global $post; |
| 23 | - $current_page_id = $post->ID; | |
| 24 | - if ($type) { | |
| 25 | - $uwp_page = uwp_get_page_id($type, false); | |
| 26 | - if ( $uwp_page && ((int) $uwp_page == $current_page_id ) ) { | |
| 27 | - return true; | |
| 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 | + } | |
| 28 | 32 | } else { |
| 29 | - return false; | |
| 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 | + } | |
| 30 | 46 | } |
| 31 | - } else { | |
| 32 | - if ($this->is_register_page() || | |
| 33 | - $this->is_login_page() || | |
| 34 | - $this->is_forgot_page() || | |
| 35 | - $this->is_change_page() || | |
| 36 | - $this->is_reset_page() || | |
| 37 | - $this->is_account_page() || | |
| 38 | - $this->is_profile_page() || | |
| 39 | - $this->is_users_page() || | |
| 40 | - $this->is_multi_register_page()) { | |
| 41 | - return true; | |
| 42 | - } else { | |
| 43 | - return false; | |
| 44 | - } | |
| 45 | 47 | } |
| 46 | - | |
| 47 | 48 | } else { |
| 48 | 49 | return false; |
| 49 | 50 | } |
| 51 | + | |
| 52 | + return false; | |
| 50 | 53 | } |
| 51 | 54 | |
| 52 | 55 | /** |
| 53 | 56 | * Checks whether the current page is register page or not. |
| @@ -137,22 +140,16 @@ | ||
| 137 | 140 | return $this->is_page('users_page'); |
| 138 | 141 | } |
| 139 | 142 | |
| 140 | 143 | /** |
| 141 | - * Checks whether the current page is multi register page or not. | |
| 144 | + * Checks whether the current page is users page or not. | |
| 142 | 145 | * |
| 143 | - * @since 1.0.0 | |
| 146 | + * @since 1.1.2 | |
| 144 | 147 | * @package userswp |
| 145 | 148 | * @return bool |
| 146 | 149 | */ |
| 147 | - public function is_multi_register_page() { | |
| 148 | - global $post; | |
| 149 | - $content = $post->post_content; | |
| 150 | - if (strpos($content, '[uwp_register role_id') !== false) { | |
| 151 | - return true; | |
| 152 | - } else { | |
| 153 | - return false; | |
| 154 | - } | |
| 150 | + public function is_user_item_page() { | |
| 151 | + return $this->is_page('user_list_item_page'); | |
| 155 | 152 | } |
| 156 | 153 | |
| 157 | 154 | /** |
| 158 | 155 | * Checks whether the current page is logged in user profile page or not. |
| @@ -187,102 +184,8 @@ | ||
| 187 | 184 | } |
| 188 | 185 | } |
| 189 | 186 | |
| 190 | 187 | /** |
| 191 | - * Gets the UsersWP page permalink based on page type. | |
| 192 | - * | |
| 193 | - * @since 1.0.0 | |
| 194 | - * @package userswp | |
| 195 | - * @param string|bool $type Page type. | |
| 196 | - * @return string Page permalink. | |
| 197 | - */ | |
| 198 | - public function get_page_permalink($type) { | |
| 199 | - $link = false; | |
| 200 | - $page_id = uwp_get_page_id($type, false); | |
| 201 | - if ($page_id) { | |
| 202 | - $link = get_permalink($page_id); | |
| 203 | - } | |
| 204 | - return $link; | |
| 205 | - } | |
| 206 | - | |
| 207 | - /** | |
| 208 | - * Gets the UsersWP register page permalink. | |
| 209 | - * | |
| 210 | - * @since 1.0.0 | |
| 211 | - * @package userswp | |
| 212 | - * @return string Page permalink. | |
| 213 | - */ | |
| 214 | - public function get_register_permalink() { | |
| 215 | - return $this->get_page_permalink('register_page'); | |
| 216 | - } | |
| 217 | - | |
| 218 | - /** | |
| 219 | - * Gets the UsersWP login page permalink. | |
| 220 | - * | |
| 221 | - * @since 1.0.0 | |
| 222 | - * @package userswp | |
| 223 | - * @return string Page permalink. | |
| 224 | - */ | |
| 225 | - public function get_login_permalink() { | |
| 226 | - return $this->get_page_permalink('login_page'); | |
| 227 | - } | |
| 228 | - | |
| 229 | - /** | |
| 230 | - * Gets the UsersWP forgot password page permalink. | |
| 231 | - * | |
| 232 | - * @since 1.0.0 | |
| 233 | - * @package userswp | |
| 234 | - * @return string Page permalink. | |
| 235 | - */ | |
| 236 | - public function get_forgot_permalink() { | |
| 237 | - return $this->get_page_permalink('forgot_page'); | |
| 238 | - } | |
| 239 | - | |
| 240 | - /** | |
| 241 | - * Gets the UsersWP reset password page permalink. | |
| 242 | - * | |
| 243 | - * @since 1.0.0 | |
| 244 | - * @package userswp | |
| 245 | - * @return string Page permalink. | |
| 246 | - */ | |
| 247 | - public function get_reset_permalink() { | |
| 248 | - return $this->get_page_permalink('reset_page'); | |
| 249 | - } | |
| 250 | - | |
| 251 | - /** | |
| 252 | - * Gets the UsersWP account page permalink. | |
| 253 | - * | |
| 254 | - * @since 1.0.0 | |
| 255 | - * @package userswp | |
| 256 | - * @return string Page permalink. | |
| 257 | - */ | |
| 258 | - public function get_account_permalink() { | |
| 259 | - return $this->get_page_permalink('account_page'); | |
| 260 | - } | |
| 261 | - | |
| 262 | - /** | |
| 263 | - * Gets the UsersWP profile page permalink. | |
| 264 | - * | |
| 265 | - * @since 1.0.0 | |
| 266 | - * @package userswp | |
| 267 | - * @return string Page permalink. | |
| 268 | - */ | |
| 269 | - public function get_profile_permalink() { | |
| 270 | - return $this->get_page_permalink('profile_page'); | |
| 271 | - } | |
| 272 | - | |
| 273 | - /** | |
| 274 | - * Gets the UsersWP users page permalink. | |
| 275 | - * | |
| 276 | - * @since 1.0.0 | |
| 277 | - * @package userswp | |
| 278 | - * @return string Page permalink. | |
| 279 | - */ | |
| 280 | - public function get_users_permalink() { | |
| 281 | - return $this->get_page_permalink('users_page'); | |
| 282 | - } | |
| 283 | - | |
| 284 | - /** | |
| 285 | 188 | * Returns all available pages as array to use in select dropdown. |
| 286 | 189 | * |
| 287 | 190 | * @since 1.0.0 |
| 288 | 191 | * @package userswp |
| @@ -329,8 +232,10 @@ | ||
| 329 | 232 | * @param string $page_title The post title. Default empty. |
| 330 | 233 | * @param mixed $page_content The post content. Default empty. |
| 331 | 234 | * @param int $post_parent Set this for the post it belongs to, if any. Default 0. |
| 332 | 235 | * @param string $status The post status. Default 'draft'. |
| 236 | + * | |
| 237 | + * @return int Page ID | |
| 333 | 238 | */ |
| 334 | 239 | public function create_page($slug, $option, $page_title = '', $page_content = '', $post_parent = 0, $status = 'publish') { |
| 335 | 240 | global $wpdb, $current_user; |
| 336 | 241 | |
| @@ -340,45 +245,72 @@ | ||
| 340 | 245 | } else { |
| 341 | 246 | $option_value = false; |
| 342 | 247 | } |
| 343 | 248 | |
| 344 | - if ($option_value > 0) : | |
| 345 | - if (get_post($option_value)) : | |
| 346 | - // Page exists | |
| 347 | - return; | |
| 348 | - endif; | |
| 349 | - endif; | |
| 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 | + } | |
| 350 | 255 | |
| 351 | - $page_found = $wpdb->get_var( | |
| 352 | - $wpdb->prepare( | |
| 353 | - "SELECT ID FROM " . $wpdb->posts . " WHERE post_name = %s LIMIT 1;", | |
| 354 | - array($slug) | |
| 355 | - ) | |
| 356 | - ); | |
| 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 | + } | |
| 357 | 264 | |
| 358 | - if ($page_found) : | |
| 359 | - // Page exists | |
| 360 | - if (!$option_value) { | |
| 361 | - $settings[$option] = $page_found; | |
| 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; | |
| 362 | 280 | update_option( 'uwp_settings', $settings ); |
| 363 | 281 | } |
| 364 | - return; | |
| 365 | - endif; | |
| 282 | + return $valid_page_found; | |
| 283 | + } | |
| 366 | 284 | |
| 367 | - $page_data = array( | |
| 368 | - 'post_status' => $status, | |
| 369 | - 'post_type' => 'page', | |
| 370 | - 'post_author' => $current_user->ID, | |
| 371 | - 'post_name' => $slug, | |
| 372 | - 'post_title' => $page_title, | |
| 373 | - 'post_content' => $page_content, | |
| 374 | - 'post_parent' => $post_parent, | |
| 375 | - 'comment_status' => 'closed' | |
| 376 | - ); | |
| 377 | - $page_id = wp_insert_post($page_data); | |
| 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 | + } | |
| 378 | 306 | |
| 379 | - $settings[$option] = $page_id; | |
| 380 | - update_option( 'uwp_settings', $settings ); | |
| 307 | + if ( $option ) { | |
| 308 | + $settings[$option] = $page_id; | |
| 309 | + update_option('uwp_settings', $settings); | |
| 310 | + } | |
| 311 | + | |
| 312 | + return $page_id; | |
| 381 | 313 | } |
| 382 | 314 | |
| 383 | 315 | /** |
| 384 | 316 | * Generates default UsersWP pages. Usually called during plugin activation. |
| @@ -396,9 +328,10 @@ | ||
| 396 | 328 | $this->create_page(esc_sql(_x('reset', 'page_slug', 'userswp')), 'reset_page', __('Reset Password', 'userswp'), '[uwp_reset]'); |
| 397 | 329 | $this->create_page(esc_sql(_x('change', 'page_slug', 'userswp')), 'change_page', __('Change Password', 'userswp'), '[uwp_change]'); |
| 398 | 330 | $this->create_page(esc_sql(_x('profile', 'page_slug', 'userswp')), 'profile_page', __('Profile', 'userswp'), '[uwp_profile]'); |
| 399 | 331 | $this->create_page(esc_sql(_x('users', 'page_slug', 'userswp')), 'users_page', __('Users', 'userswp'), '[uwp_users]'); |
| 400 | - | |
| 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 | + | |
| 401 | 334 | } |
| 402 | 335 | |
| 403 | 336 | |
| 404 | 337 | /** |
| @@ -407,15 +340,10 @@ | ||
| 407 | 340 | * @since 1.0.0 |
| 408 | 341 | * @package userswp |
| 409 | 342 | * |
| 410 | 343 | * @param int $blog_id Blog ID. |
| 411 | - * @param int $user_id User ID. | |
| 412 | - * @param string $domain Site domain. | |
| 413 | - * @param string $path Site path. | |
| 414 | - * @param int $site_id Site ID. Only relevant on multi-network installs. | |
| 415 | - * @param array $meta Meta data. Used to set initial site options. | |
| 416 | 344 | */ |
| 417 | - public function wpmu_generate_default_pages_on_new_site( $blog_id, $user_id, $domain, $path, $site_id, $meta ) { | |
| 345 | + public function wpmu_generate_default_pages_on_new_site( $blog_id ) { | |
| 418 | 346 | |
| 419 | 347 | if (uwp_get_installation_type() != 'multi_na_all') { |
| 420 | 348 | return; |
| 421 | 349 | } |
| @@ -431,9 +359,9 @@ | ||
| 431 | 359 | |
| 432 | 360 | // Switch to the new blog. |
| 433 | 361 | switch_to_blog( $blog_id ); |
| 434 | 362 | |
| 435 | - uwp_generate_default_pages(); | |
| 363 | + $this->generate_default_pages(); | |
| 436 | 364 | |
| 437 | 365 | // Restore original blog. |
| 438 | 366 | restore_current_blog(); |
| 439 | 367 | } |
| @@ -446,12 +374,9 @@ | ||
| 446 | 374 | * @param string $page Page type. |
| 447 | 375 | * @return string Page permalink. |
| 448 | 376 | */ |
| 449 | 377 | public function get_page_link($page) { |
| 450 | - | |
| 451 | 378 | $link = ""; |
| 452 | - $page_id = false; | |
| 453 | - | |
| 454 | 379 | switch ($page) { |
| 455 | 380 | case 'register': |
| 456 | 381 | $page_id = uwp_get_page_id('register_page', false); |
| 457 | 382 | break; |
| @@ -474,8 +399,12 @@ | ||
| 474 | 399 | |
| 475 | 400 | case 'users': |
| 476 | 401 | $page_id = uwp_get_page_id('users_page', false); |
| 477 | 402 | break; |
| 403 | + | |
| 404 | + default: | |
| 405 | + $page_id = uwp_get_page_id($page, false); | |
| 406 | + break; | |
| 478 | 407 | } |
| 479 | 408 | |
| 480 | 409 | if ($page_id) { |
| 481 | 410 | $link = get_permalink($page_id); |
| @@ -480,8 +409,12 @@ | ||
| 480 | 409 | if ($page_id) { |
| 481 | 410 | $link = get_permalink($page_id); |
| 482 | 411 | } |
| 483 | 412 | |
| 413 | + if ( ! empty( $link ) && function_exists( 'PLL' ) ) { | |
| 414 | + $link = PLL()->links_model->add_language_to_link( $link, PLL()->curlang ); | |
| 415 | + } | |
| 416 | + | |
| 484 | 417 | return $link; |
| 485 | 418 | } |
| 486 | 419 | |
| 487 | 420 | /** |
| @@ -498,9 +431,9 @@ | ||
| 498 | 431 | * @return string Built profile page link. |
| 499 | 432 | */ |
| 500 | 433 | public function build_profile_tab_url($user_id, $tab = false, $subtab = false) { |
| 501 | 434 | |
| 502 | - $link = apply_filters('uwp_profile_link', get_author_posts_url($user_id), $user_id); | |
| 435 | + $link = apply_filters('uwp_profile_link', get_author_posts_url($user_id), $user_id); | |
| 503 | 436 | |
| 504 | 437 | if ($link != '') { |
| 505 | 438 | if (isset($_REQUEST['page_id'])) { |
| 506 | 439 | $permalink_structure = 'DEFAULT'; |
| @@ -527,20 +460,27 @@ | ||
| 527 | 460 | } |
| 528 | 461 | } |
| 529 | 462 | } |
| 530 | 463 | |
| 531 | - return $link; | |
| 464 | + return trailingslashit( $link ); | |
| 532 | 465 | } |
| 533 | 466 | |
| 534 | - | |
| 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 | + */ | |
| 535 | 475 | public function get_page_id($type, $get_link = false) { |
| 536 | 476 | |
| 537 | 477 | $link = false; |
| 538 | - $page_id = uwp_get_option($type, false); | |
| 478 | + $page_id = uwp_get_option($type, false, false); | |
| 539 | 479 | |
| 540 | - if ($page_id) { | |
| 480 | + if ($page_id && $page_id > 0) { | |
| 541 | 481 | if (uwp_is_wpml()) { |
| 542 | - $wpml_page_id = uwp_wpml_object_id($page_id, 'page', true, ICL_LANGUAGE_CODE); | |
| 482 | + $wpml_page_id = uwp_wpml_object_id($page_id, 'page', true); | |
| 543 | 483 | if (!empty($wpml_page_id)) { |
| 544 | 484 | $page_id = $wpml_page_id; |
| 545 | 485 | } |
| 546 | 486 | } |
| @@ -551,7 +491,55 @@ | ||
| 551 | 491 | } |
| 552 | 492 | } |
| 553 | 493 | |
| 554 | 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; | |
| 555 | 543 | } |
| 556 | 544 | |
| 557 | 545 | } |