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