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

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