PluginProbe
Elementor Website Builder – more than just a page builder / 4.1.3
Elementor Website Builder – more than just a page builder v4.1.3
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 4.1.3, at modules/landing-pages/module.php

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