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

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