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

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