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

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