PluginProbe
Elementor Website Builder – more than just a page builder / 3.5.0-dev9
Elementor Website Builder – more than just a page builder v3.5.0-dev9
4.3.0-beta3 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 All 452 releases
elementor / modules / landing-pages / module.php

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

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