PluginProbe
Elementor Website Builder – more than just a page builder / 3.32.2
Elementor Website Builder – more than just a page builder v3.32.2
4.3.0-beta2 4.3.0-beta1 4.2.4 4.2.3 4.2.2 4.2.1 4.2.0 4.1.5 4.2.0-beta2 4.2.0-dev2 4.2.0-beta1 4.1.4 4.1.3 4.1.2 4.1.1 4.1.0 4.1.0-beta3 4.1.0-dev3 4.0.9 4.1.0-beta2 4.1.0-dev2 4.0.8 4.1.0-beta1 4.1.0-dev1 4.0.7 All 451 releases
elementor / modules / landing-pages / module.php

module.php in Elementor Website Builder – more than just a page builder 3.32.2, at modules/landing-pages/module.php

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