module.php (16943B)
1 <?php 2 namespace Elementor\Modules\LandingPages; 3 4 use Elementor\Core\Base\Module as BaseModule; 5 use Elementor\Core\Documents_Manager; 6 use Elementor\Core\Experiments\Manager as Experiments_Manager; 7 use Elementor\Modules\LandingPages\Documents\Landing_Page; 8 use Elementor\Modules\LandingPages\Module as Landing_Pages_Module; 9 use Elementor\Plugin; 10 use Elementor\TemplateLibrary\Source_Local; 11 use Elementor\Utils; 12 13 if ( ! defined( 'ABSPATH' ) ) { 14 exit; // Exit if accessed directly. 15 } 16 17 class Module extends BaseModule { 18 19 const DOCUMENT_TYPE = 'landing-page'; 20 const CPT = 'e-landing-page'; 21 const ADMIN_PAGE_SLUG = 'edit.php?post_type=' . self::CPT; 22 23 private $posts; 24 private $trashed_posts; 25 private $new_lp_url; 26 private $permalink_structure; 27 28 public function get_name() { 29 return 'landing-pages'; 30 } 31 32 /** 33 * Get Experimental Data 34 * 35 * Implementation of this method makes the module an experiment. 36 * 37 * @since 3.1.0 38 * 39 * @return array 40 */ 41 public static function get_experimental_data() { 42 return [ 43 'name' => 'landing-pages', 44 'title' => esc_html__( 'Landing Pages', 'elementor' ), 45 'description' => esc_html__( 'Adds a new Elementor content type that allows creating beautiful landing pages instantly in a streamlined workflow.', 'elementor' ), 46 'release_status' => Experiments_Manager::RELEASE_STATUS_BETA, 47 'default' => Experiments_Manager::STATE_ACTIVE, 48 ]; 49 } 50 51 /** 52 * Get Trashed Landing Pages Posts 53 * 54 * Returns the posts property of a WP_Query run for Landing Pages with post_status of 'trash'. 55 * 56 * @since 3.1.0 57 * 58 * @return array trashed posts 59 */ 60 private function get_trashed_landing_page_posts() { 61 if ( $this->trashed_posts ) { 62 return $this->trashed_posts; 63 } 64 65 // `'posts_per_page' => 1` is because this is only used as an indicator to whether there are any trashed landing pages. 66 $trashed_posts_query = new \WP_Query( [ 67 'post_type' => self::CPT, 68 'post_status' => 'trash', 69 'posts_per_page' => 1, 70 'meta_key' => '_elementor_template_type', 71 'meta_value' => self::DOCUMENT_TYPE, 72 ] ); 73 74 $this->trashed_posts = $trashed_posts_query->posts; 75 76 return $this->trashed_posts; 77 } 78 79 /** 80 * Get Landing Pages Posts 81 * 82 * Returns the posts property of a WP_Query run for posts with the Landing Pages CPT. 83 * 84 * @since 3.1.0 85 * 86 * @return array posts 87 */ 88 private function get_landing_page_posts() { 89 if ( $this->posts ) { 90 return $this->posts; 91 } 92 93 // `'posts_per_page' => 1` is because this is only used as an indicator to whether there are any landing pages. 94 $posts_query = new \WP_Query( [ 95 'post_type' => self::CPT, 96 'post_status' => 'any', 97 'posts_per_page' => 1, 98 'meta_key' => '_elementor_template_type', 99 'meta_value' => self::DOCUMENT_TYPE, 100 ] ); 101 102 $this->posts = $posts_query->posts; 103 104 return $this->posts; 105 } 106 107 /** 108 * Is Elementor Landing Page. 109 * 110 * Check whether the post is an Elementor Landing Page. 111 * 112 * @since 3.1.0 113 * @access public 114 * 115 * @param \WP_Post $post Post Object 116 * 117 * @return bool Whether the post was built with Elementor. 118 */ 119 public function is_elementor_landing_page( $post ) { 120 return self::CPT === $post->post_type; 121 } 122 123 /** 124 * Add Submenu Page 125 * 126 * Adds the 'Landing Pages' submenu item to the 'Templates' menu item. 127 * 128 * @since 3.1.0 129 */ 130 private function add_submenu_page() { 131 $posts = $this->get_landing_page_posts(); 132 133 // If there are no Landing Pages, show the "Create Your First Landing Page" page. 134 // If there are, show the pages table. 135 if ( ! empty( $posts ) ) { 136 $landing_page_menu_slug = self::ADMIN_PAGE_SLUG; 137 $landing_page_menu_callback = null; 138 } else { 139 $landing_page_menu_slug = self::CPT; 140 $landing_page_menu_callback = [ $this, 'print_empty_landing_pages_page' ]; 141 } 142 143 $landing_pages_title = esc_html__( 'Landing Pages', 'elementor' ); 144 145 add_submenu_page( 146 Source_Local::ADMIN_MENU_SLUG, 147 $landing_pages_title, 148 $landing_pages_title, 149 'manage_options', 150 $landing_page_menu_slug, 151 $landing_page_menu_callback 152 ); 153 } 154 155 /** 156 * Get 'Add New' Landing Page URL 157 * 158 * Retrieves the custom URL for the admin dashboard's 'Add New' button in the Landing Pages admin screen. This URL 159 * creates a new Landing Pages and directly opens the Elementor Editor with the Template Library modal open on the 160 * Landing Pages tab. 161 * 162 * @since 3.1.0 163 * 164 * @return string 165 */ 166 private function get_add_new_landing_page_url() { 167 if ( ! $this->new_lp_url ) { 168 $this->new_lp_url = Plugin::$instance->documents->get_create_new_post_url( self::CPT, self::DOCUMENT_TYPE ) . '#library'; 169 } 170 return $this->new_lp_url; 171 } 172 173 /** 174 * Get Empty Landing Pages Page 175 * 176 * Prints the HTML content of the page that is displayed when there are no existing landing pages in the DB. 177 * Added as the callback to add_submenu_page. 178 * 179 * @since 3.1.0 180 */ 181 public function print_empty_landing_pages_page() { 182 $template_sources = Plugin::$instance->templates_manager->get_registered_sources(); 183 $source_local = $template_sources['local']; 184 $trashed_posts = $this->get_trashed_landing_page_posts(); 185 186 ?> 187 <div class="e-landing-pages-empty"> 188 <?php 189 /** @var Source_Local $source_local */ 190 $source_local->print_blank_state_template( esc_html__( 'Landing Page', 'elementor' ), $this->get_add_new_landing_page_url(), esc_html__( 'Build Effective Landing Pages for your business\' marketing campaigns.', 'elementor' ) ); 191 192 if ( ! empty( $trashed_posts ) ) : ?> 193 <div class="e-trashed-items"> 194 <?php 195 printf( 196 /* translators: %1$s Link open tag, %2$s: Link close tag. */ 197 esc_html__( 'Or view %1$sTrashed Items%1$s', 'elementor' ), 198 '<a href="' . esc_url( admin_url( 'edit.php?post_status=trash&post_type=' . self::CPT ) ) . '">', 199 '</a>' 200 ); 201 ?> 202 </div> 203 <?php endif; ?> 204 </div> 205 <?php 206 } 207 208 /** 209 * Is Current Admin Page Edit LP 210 * 211 * Checks whether the current page is a native WordPress edit page for a landing page. 212 */ 213 private function is_landing_page_admin_edit() { 214 $screen = get_current_screen(); 215 216 if ( 'post' === $screen->base ) { 217 return $this->is_elementor_landing_page( get_post() ); 218 } 219 220 return false; 221 } 222 223 /** 224 * Admin Localize Settings 225 * 226 * Enables adding properties to the globally available elementorAdmin.config JS object in the Admin Dashboard. 227 * Runs on the 'elementor/admin/localize_settings' filter. 228 * 229 * @since 3.1.0 230 * 231 * @param $settings 232 * @return array|null 233 */ 234 private function admin_localize_settings( $settings ) { 235 $additional_settings = [ 236 'urls' => [ 237 'addNewLandingPageUrl' => $this->get_add_new_landing_page_url(), 238 ], 239 'landingPages' => [ 240 'landingPagesHasPages' => [] !== $this->get_landing_page_posts(), 241 'isLandingPageAdminEdit' => $this->is_landing_page_admin_edit(), 242 ], 243 ]; 244 245 return array_replace_recursive( $settings, $additional_settings ); 246 } 247 248 /** 249 * Register Landing Pages CPT 250 * 251 * @since 3.1.0 252 */ 253 private function register_landing_page_cpt() { 254 $labels = [ 255 'name' => esc_html__( 'Landing Pages', 'elementor' ), 256 'singular_name' => esc_html__( 'Landing Page', 'elementor' ), 257 'add_new' => esc_html__( 'Add New', 'elementor' ), 258 'add_new_item' => esc_html__( 'Add New Landing Page', 'elementor' ), 259 'edit_item' => esc_html__( 'Edit Landing Page', 'elementor' ), 260 'new_item' => esc_html__( 'New Landing Page', 'elementor' ), 261 'all_items' => esc_html__( 'All Landing Pages', 'elementor' ), 262 'view_item' => esc_html__( 'View Landing Page', 'elementor' ), 263 'search_items' => esc_html__( 'Search Landing Pages', 'elementor' ), 264 'not_found' => esc_html__( 'No landing pages found', 'elementor' ), 265 'not_found_in_trash' => esc_html__( 'No landing pages found in trash', 'elementor' ), 266 'parent_item_colon' => '', 267 'menu_name' => esc_html__( 'Landing Pages', 'elementor' ), 268 ]; 269 270 $args = [ 271 'labels' => $labels, 272 'public' => true, 273 'show_in_menu' => 'edit.php?post_type=elementor_library&tabs_group=library', 274 'capability_type' => 'page', 275 'taxonomies' => [ Source_Local::TAXONOMY_TYPE_SLUG ], 276 'supports' => [ 'title', 'editor', 'comments', 'revisions', 'trackbacks', 'author', 'excerpt', 'page-attributes', 'thumbnail', 'custom-fields', 'post-formats', 'elementor' ], 277 ]; 278 279 register_post_type( self::CPT, $args ); 280 } 281 282 /** 283 * Remove Post Type Slug 284 * 285 * Landing Pages are supposed to act exactly like pages. This includes their URLs being directly under the site's 286 * domain name. Since "Landing Pages" is a CPT, WordPress automatically adds the landing page slug as a prefix to 287 * it's posts' permalinks. This method checks if the post's post type is Landing Pages, and if it is, it removes 288 * the CPT slug from the requested post URL. 289 * 290 * Runs on the 'post_type_link' filter. 291 * 292 * @since 3.1.0 293 * 294 * @param $post_link 295 * @param $post 296 * @param $leavename 297 * @return string|string[] 298 */ 299 private function remove_post_type_slug( $post_link, $post, $leavename ) { 300 // Only try to modify the permalink if the post is a Landing Page. 301 if ( self::CPT !== $post->post_type || 'publish' !== $post->post_status ) { 302 return $post_link; 303 } 304 305 // Any slug prefixes need to be removed from the post link. 306 return get_home_url() . '/' . $post->post_name . '/'; 307 } 308 309 /** 310 * Adjust Landing Page Query 311 * 312 * Since Landing Pages are a CPT but should act like pages, the WP_Query that is used to fetch the page from the 313 * database needs to be adjusted. This method adds the Landing Pages CPT to the list of queried post types, to 314 * make sure the database query finds the correct Landing Page to display. 315 * Runs on the 'pre_get_posts' action. 316 * 317 * @since 3.1.0 318 * 319 * @param \WP_Query $query 320 */ 321 private function adjust_landing_page_query( \WP_Query $query ) { 322 // Only handle actual pages. 323 if ( 324 ! $query->is_main_query() 325 // If the query is not for a page. 326 || ! isset( $query->query['page'] ) 327 // If the query is for a static home/blog page. 328 || is_home() 329 // If the post type comes already set, the main query is probably a custom one made by another plugin. 330 // In this case we do not want to intervene in order to not cause a conflict. 331 || isset( $query->query['post_type'] ) 332 ) { 333 return; 334 } 335 336 // Create the post types property as an array and include the landing pages CPT in it. 337 $query_post_types = [ 'post', 'page', self::CPT ]; 338 339 // Since WordPress determined this is supposed to be a page, we'll pre-set the post_type query arg to make sure 340 // it includes the Landing Page CPT, so when the query is parsed, our CPT will be a legitimate match to the 341 // Landing Page's permalink (that is directly under the domain, without a CPT slug prefix). In some cases, 342 // The 'name' property will be set, and in others it is the 'pagename', so we have to cover both cases. 343 if ( ! empty( $query->query['name'] ) ) { 344 $query->set( 'post_type', $query_post_types ); 345 } elseif ( ! empty( $query->query['pagename'] ) && false === strpos( $query->query['pagename'], '/' ) ) { 346 $query->set( 'post_type', $query_post_types ); 347 348 // We also need to set the name query var since redirect_guess_404_permalink() relies on it. 349 add_filter( 'pre_redirect_guess_404_permalink', function( $value ) use ( $query ) { 350 set_query_var( 'name', $query->query['pagename'] ); 351 352 return $value; 353 } ); 354 } 355 } 356 357 /** 358 * Handle 404 359 * 360 * This method runs after a page is not found in the database, but before a page is returned as a 404. 361 * These cases are handled in this filter callback, that runs on the 'pre_handle_404' filter. 362 * 363 * In some cases (such as when a site uses custom permalink structures), WordPress's WP_Query does not identify a 364 * Landing Page's URL as a post belonging to the Landing Page CPT. Some cases are handled successfully by the 365 * adjust_landing_page_query() method, but some are not and still trigger a 404 process. This method handles such 366 * cases by overriding the $wp_query global to fetch the correct landing page post entry. 367 * 368 * For example, since Landing Pages slugs come directly after the site domain name, WP_Query might parse the post 369 * as a category page. Since there is no category matching the slug, it triggers a 404 process. In this case, we 370 * run a query for a Landing Page post with the passed slug ($query->query['category_name']. If a Landing Page 371 * with the passed slug is found, we override the global $wp_query with the new, correct query. 372 * 373 * @param $current_value 374 * @param $query 375 * @return false 376 */ 377 private function handle_404( $current_value, $query ) { 378 global $wp_query; 379 380 // If another plugin/theme already used this filter, exit here to avoid conflicts. 381 if ( $current_value ) { 382 return $current_value; 383 } 384 385 if ( 386 // Make sure we only intervene in the main query. 387 ! $query->is_main_query() 388 // If a post was found, this is not a 404 case, so do not intervene. 389 || ! empty( $query->posts ) 390 // This filter is only meant to deal with wrong queries where the only query var is 'category_name'. 391 // If there is no 'category_name' query var, do not intervene. 392 || empty( $query->query['category_name'] ) 393 // If the query is for a real taxonomy (determined by it including a table to search in, such as the 394 // wp_term_relationships table), do not intervene. 395 || ! empty( $query->tax_query->table_aliases ) 396 ) { 397 return false; 398 } 399 400 // Search for a Landing Page with the same name passed as the 'category name'. 401 $possible_new_query = new \WP_Query( [ 402 'post_type' => self::CPT, 403 'name' => $query->query['category_name'], 404 ] ); 405 406 // Only if such a Landing Page is found, override the query to fetch the correct page. 407 if ( ! empty( $possible_new_query->posts ) ) { 408 $wp_query = $possible_new_query; //phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited 409 } 410 411 return false; 412 } 413 414 public function __construct() { 415 $this->permalink_structure = get_option( 'permalink_structure' ); 416 417 $this->register_landing_page_cpt(); 418 419 // If there is a permalink structure set to the site, run the hooks that modify the Landing Pages permalinks to 420 // match WordPress' native 'Pages' post type. 421 if ( '' !== $this->permalink_structure ) { 422 // Landing Pages' post link needs to be modified to be identical to the pages permalink structure. This 423 // needs to happen in both the admin and the front end, since post links are also used in the admin pages. 424 add_filter( 'post_type_link', function( $post_link, $post, $leavename ) { 425 return $this->remove_post_type_slug( $post_link, $post, $leavename ); 426 }, 10, 3 ); 427 428 // The query itself only has to be manipulated when pages are viewed in the front end. 429 if ( ! is_admin() || wp_doing_ajax() ) { 430 add_action( 'pre_get_posts', function ( $query ) { 431 $this->adjust_landing_page_query( $query ); 432 } ); 433 434 // Handle cases where visiting a Landing Page's URL returns 404. 435 add_filter( 'pre_handle_404', function ( $value, $query ) { 436 return $this->handle_404( $value, $query ); 437 }, 10, 2 ); 438 } 439 } 440 441 add_action( 'elementor/documents/register', function( Documents_Manager $documents_manager ) { 442 $documents_manager->register_document_type( self::DOCUMENT_TYPE, Landing_Page::get_class_full_name() ); 443 } ); 444 445 add_action( 'admin_menu', function() { 446 $this->add_submenu_page(); 447 }, 30 ); 448 449 // Add the custom 'Add New' link for Landing Pages into Elementor's admin config. 450 add_action( 'elementor/admin/localize_settings', function( array $settings ) { 451 return $this->admin_localize_settings( $settings ); 452 } ); 453 454 add_filter( 'elementor/template_library/sources/local/register_taxonomy_cpts', function( array $cpts ) { 455 $cpts[] = self::CPT; 456 457 return $cpts; 458 } ); 459 460 // In the Landing Pages Admin Table page - Overwrite Template type column header title. 461 add_action( 'manage_' . Landing_Pages_Module::CPT . '_posts_columns', function( $posts_columns ) { 462 /** @var Source_Local $source_local */ 463 $source_local = Plugin::$instance->templates_manager->get_source( 'local' ); 464 465 return $source_local->admin_columns_headers( $posts_columns ); 466 } ); 467 468 // In the Landing Pages Admin Table page - Overwrite Template type column row values. 469 add_action( 'manage_' . Landing_Pages_Module::CPT . '_posts_custom_column', function( $column_name, $post_id ) { 470 /** @var Landing_Page $document */ 471 $document = Plugin::$instance->documents->get( $post_id ); 472 473 $document->admin_columns_content( $column_name ); 474 }, 10, 2 ); 475 476 // Overwrite the Admin Bar's 'New +' Landing Page URL with the link that creates the new LP in Elementor 477 // with the Template Library modal open. 478 add_action( 'admin_bar_menu', function( $admin_bar ) { 479 // Get the Landing Page menu node. 480 $new_landing_page_node = $admin_bar->get_node( 'new-e-landing-page' ); 481 482 if ( $new_landing_page_node ) { 483 $new_landing_page_node->href = $this->get_add_new_landing_page_url(); 484 485 $admin_bar->add_node( $new_landing_page_node ); 486 } 487 }, 100 ); 488 } 489 }