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

320 lines 9.4 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\Modules\LandingPages\Documents\Landing_Page;
7 use Elementor\Plugin;
8 use Elementor\TemplateLibrary\Source_Local;
9 use Elementor\User;
10 use Elementor\Utils;
11
12 if ( ! defined( 'ABSPATH' ) ) {
13 exit; // Exit if accessed directly.
14 }
15
16 class Module extends BaseModule {
17
18 const DOCUMENT_TYPE = 'landing-page';
19 const ADMIN_PAGE_SLUG = 'edit.php?post_type=page&elementor_library_type=landing-page';
20 const ADMIN_PAGE_NO_LPS_SLUG = 'edit.php?post_type=elementor_library&page=landing-page';
21
22 private $posts;
23 private $trashed_posts;
24
25 public function get_name() {
26 return 'landing-pages';
27 }
28
29 public static function get_experimental_data() {
30 return [
31 'name' => 'landing-pages',
32 'title' => __( 'Landing Pages', 'elementor' ),
33 'description' => __( 'Adds a new Elementor content type that allows creating beautiful landing pages instantly in a streamlined workflow.', 'elementor' ),
34 ];
35 }
36
37 private function get_trashed_landing_page_posts() {
38 if ( isset( $this->trashed_posts ) ) {
39 return $this->trashed_posts;
40 }
41
42 $this->trashed_posts = new \WP_Query( [
43 'post_type' => 'page',
44 'post_status' => 'trash',
45 'elementor_library_type' => self::DOCUMENT_TYPE,
46 'meta_key' => '_elementor_template_type',
47 'meta_value' => 'landing-page',
48 ] );
49
50 return $this->trashed_posts;
51 }
52
53 private function get_landing_page_posts() {
54 if ( isset( $this->posts ) ) {
55 return $this->posts;
56 }
57
58 $this->posts = new \WP_Query( [
59 'post_type' => 'page',
60 'elementor_library_type' => self::DOCUMENT_TYPE,
61 'meta_key' => '_elementor_template_type',
62 'meta_value' => 'landing-page',
63 ] );
64
65 return $this->posts;
66 }
67
68 /**
69 * Add Submenu Page
70 *
71 * Adds the 'Landing Pages' submenu item to the 'Templates' menu item.
72 *
73 * @since 3.1.0
74 */
75 private function add_submenu_page() {
76 $posts = $this->get_landing_page_posts();
77
78 // If there are no Landing Pages, show the "Create Your First Landing Page" page.
79 // If there are, show the pages table.
80 if ( ! empty( $posts->posts ) ) {
81 $this->landing_page_menu_slug = self::ADMIN_PAGE_SLUG;
82 $landing_page_menu_callback = null;
83 } else {
84 $this->landing_page_menu_slug = self::DOCUMENT_TYPE;
85 $landing_page_menu_callback = [ $this, 'print_empty_landing_pages_page' ];
86 }
87
88 $landing_pages_title = __( 'Landing Pages', 'elementor' );
89
90 add_submenu_page(
91 Source_Local::ADMIN_MENU_SLUG,
92 $landing_pages_title,
93 $landing_pages_title,
94 'manage_options',
95 $this->landing_page_menu_slug,
96 $landing_page_menu_callback
97 );
98 }
99
100 private function get_add_new_landing_page_url() {
101 return Utils::get_create_new_post_url( 'page', self::DOCUMENT_TYPE ) . '#library';
102 }
103
104 /**
105 * Remove Type Column
106 *
107 * Removes the "Type" column from the pages table.
108 *
109 * @since 3.1.0
110 *
111 * @param array $columns
112 * @return array $columns
113 */
114 private function remove_type_column( $columns ) {
115 unset( $columns['taxonomy-elementor_library_type'] );
116
117 return $columns;
118 }
119
120 /**
121 * Get Empty Landing Pages Page
122 *
123 * Prints the HTML content of the page that is displayed when there are no existing landing pages in the DB.
124 * Added as the callback to add_submenu_page.
125 *
126 * @since 3.1.0
127 */
128 public function print_empty_landing_pages_page() {
129 $template_sources = Plugin::$instance->templates_manager->get_registered_sources();
130 $source_local = $template_sources['local'];
131 $trashed_posts = $this->get_trashed_landing_page_posts();
132
133 ?>
134 <div class="e-landing-pages-empty">
135 <?php
136 /** @var Source_Local $source_local */
137 $source_local->print_blank_state_template( __( 'Landing Page', 'elementor' ), $this->get_add_new_landing_page_url(), __( 'Build Effective Landing Pages for your business\' marketing campaigns.', 'elementor' ) );
138
139 if ( ! empty( $trashed_posts->posts ) ) { ?>
140 <div class="e-trashed-items">
141 <?php echo sprintf( __( 'Or view <a href="%s">Trashed Items</a>', 'elementor' ), admin_url( 'edit.php?post_status=trash&post_type=page&elementor_library_type=landing-page' ) ); ?>
142 </div>
143 <?php } ?>
144 </div>
145 <?php
146 }
147
148 /**
149 * Add Finder Items
150 *
151 * Adds Items to the Finder index to make them searchable in the Elementor Editor Finder modal.
152 *
153 * @since 3.1.0
154 *
155 * @param array $categories
156 * @return array $categories
157 */
158 private function add_finder_items( array $categories ) {
159 $categories['general']['items']['landing-pages'] = [
160 'title' => __( 'Landing Pages', 'elementor' ),
161 'icon' => 'single-page',
162 'url' => admin_url( self::ADMIN_PAGE_SLUG ),
163 'keywords' => [ self::DOCUMENT_TYPE, 'landing', 'page', 'library' ],
164 ];
165
166 $categories['create']['items']['landing-pages'] = [
167 'title' => __( 'Add New Landing Page', 'elementor' ),
168 'icon' => 'single-page',
169 'url' => esc_url( Utils::get_create_new_post_url( 'page', self::DOCUMENT_TYPE ) ) . '#library',
170 'keywords' => [ 'landing-page', 'landing', 'page', 'new', 'create', 'library' ],
171 ];
172
173 return $categories;
174 }
175
176 /**
177 * Change Admin Page Title
178 *
179 * Changes the page title of the "Landing Pages" Admin page which displays the "Pages" table,
180 * from "Pages" to the "Landing Pages" translation string.
181 *
182 * @since 3.1.0
183 */
184 private function change_admin_page_title() {
185 global $wp_post_types;
186
187 if ( $this->is_landing_pages_page() ) {
188 $wp_post_types['page']->labels->name = __( 'Landing Pages', 'elementor' );
189 }
190 }
191
192 /**
193 * Change Admin Meta Title
194 *
195 * Changes the contents of the meta <title> tag in the "Landing Pages" Admin page which displays the "Pages" table,
196 * from "Pages" to "Landing Pages".
197 *
198 * @since 3.1.0
199 *
200 * @param string $title meta title contents
201 * @return string $title
202 */
203 private function change_admin_meta_title( $title ) {
204 if ( $this->is_landing_pages_page() ) {
205 // Get WordPress' default 'Pages' translation string, since the original title comes from WordPress core.
206 return str_replace( __( 'Pages', 'elementor' ), __( 'Landing Pages', 'elementor' ), $title ); // phpcs:ignore WordPress.WP.I18n.TextDomainMismatch
207 }
208
209 return $title;
210 }
211
212 private function is_landing_pages_page() {
213 global $wp_the_query;
214
215 return isset( $wp_the_query->query['post_type'] )
216 && isset( $wp_the_query->query['elementor_library_type'] )
217 && 'page' === $wp_the_query->query['post_type']
218 && self::DOCUMENT_TYPE === $wp_the_query->query['elementor_library_type'];
219 }
220
221 /**
222 * Add Landing Page post state.
223 *
224 * Adds a new "Landing Page" post state to the post table.
225 *
226 * Fired by `display_post_states` filter.
227 *
228 * @since 1.8.0
229 * @access public
230 *
231 * @param array $post_states An array of post display states.
232 * @param \WP_Post $post The current post object.
233 *
234 * @return array A filtered array of post display states.
235 */
236 public function add_landing_page_post_state( $post_states, $post ) {
237 if ( User::is_current_user_can_edit( $post->ID ) && Plugin::$instance->db->is_elementor_landing_page( $post ) ) {
238 $post_states['landing-page'] = __( 'Landing Page', 'elementor' );
239 }
240
241 return $post_states;
242 }
243
244 /**
245 * Is Current Admin Page Edit LP
246 *
247 * Checks whether the current page is a native WordPress edit page for a landing page.
248 */
249 private function is_current_admin_page_edit_lp() {
250 $screen = get_current_screen();
251
252 if ( 'post' === $screen->base ) {
253 return Plugin::$instance->db->is_elementor_landing_page( get_post() );
254 }
255
256 return false;
257 }
258
259 private function admin_localize_settings( $settings ) {
260 $additional_settings = [
261 'urls' => [
262 'addNewLandingPageUrl' => $this->get_add_new_landing_page_url(),
263 ],
264 'landingPages' => [
265 'landingPagesHasPages' => [] !== $this->get_landing_page_posts(),
266 'isCurrentPageLPAdminEdit' => $this->is_current_admin_page_edit_lp(),
267 ],
268 ];
269
270 return array_replace_recursive( $settings, $additional_settings );
271 }
272
273 public function __construct() {
274 add_action( 'elementor/documents/register', function( Documents_Manager $documents_manager ) {
275 $documents_manager->register_document_type( self::DOCUMENT_TYPE, Landing_Page::get_class_full_name() );
276 } );
277
278 add_action( 'admin_menu', function() {
279 $this->add_submenu_page();
280 }, 30 );
281
282 // This is a hack to change the H1 title of the Landing Pages table page from 'Pages' to 'Landing Pages'.
283 add_action( 'admin_head', function() {
284 $this->change_admin_page_title();
285 } );
286
287 // When visiting the Landing Pages Table page, the default title is 'Pages'. This filter callback changes it
288 // to the 'Landing Pages' translation string.
289 add_filter( 'admin_title', function( $title ) {
290 return $this->change_admin_meta_title( $title );
291 } );
292
293 add_action( 'parent_file', function( $parent_file ) {
294 global $current_screen;
295
296 if ( 'post' === $current_screen->base && Plugin::$instance->db->is_elementor_landing_page( get_post() ) ) {
297 return Source_Local::ADMIN_MENU_SLUG;
298 }
299
300 return $parent_file;
301 } );
302
303 // Add the custom 'Add New' link for Landing Pages into Elementor's admin config.
304 add_action( 'elementor/admin/localize_settings', function( $settings ) {
305 return $this->admin_localize_settings( $settings );
306 } );
307
308 add_filter( 'elementor/finder/categories', function( $categories ) {
309 return $this->add_finder_items( $categories );
310 } );
311
312 // Remove the "Type" column added to the Pages table by the Landing Page document.
313 add_filter( 'manage_page_posts_columns', function( $columns ) {
314 return $this->remove_type_column( $columns );
315 } );
316
317 add_filter( 'display_post_states', [ $this, 'add_landing_page_post_state' ], 20, 2 );
318 }
319 }
320