PluginProbe
Hello Plus / 1.2.0
Hello Plus v1.2.0
trunk 1.0.0 1.0.1 1.0.2 1.0.3 1.1.0 1.1.1 1.1.2 1.1.3 1.2.0 1.2.1 1.3.0 1.4.0 1.4.1 1.5.0 1.5.1 1.5.2 1.5.3 1.6.0 1.6.1 1.6.2 1.7.0 1.7.1 1.7.2 1.7.3 All 30 releases
hello-plus / modules / template-parts / documents / ehp-document-base.php

ehp-document-base.php in Hello Plus 1.2.0, at modules/template-parts/documents/ehp-document-base.php

226 lines 6.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace HelloPlus\Modules\TemplateParts\Documents;
4
5 if ( ! defined( 'ABSPATH' ) ) {
6 exit; // Exit if accessed directly.
7 }
8
9 use HelloPlus\Includes\Utils;
10 use Elementor\{
11 TemplateLibrary\Source_Local,
12 Modules\Library\Documents\Library_Document
13 };
14
15 use HelloPlus\Includes\Utils as Theme_Utils;
16 use Elementor\Modules\PageTemplates\Module as Page_Templates_Module;
17 use WP_Query;
18
19 /**
20 * class Ehp_Document_Base
21 **/
22 abstract class Ehp_Document_Base extends Library_Document {
23
24 const LOCATION = '';
25
26 public static function get_properties(): array {
27 $properties = parent::get_properties();
28 $properties['support_kit'] = true;
29 $properties['show_in_finder'] = true;
30 $properties['support_site_editor'] = false;
31 $properties['allow_adding_widgets'] = false;
32 $properties['show_navigator'] = false;
33 $properties['support_page_layout'] = false;
34 $properties['allow_closing_remote_library'] = false;
35 $properties['library_close_title'] = esc_html__( 'Go To Dashboard', 'hello-plus' );
36 $properties['publish_button_title'] = esc_html__( 'After publishing this widget, you will be able to set it as visible on the entire site in the Admin Table.', 'hello-plus' );
37 /**
38 * Filter the document properties.
39 *
40 * @param array $properties The document default properties.
41 *
42 * @since 1.0.0
43 *
44 */
45 return apply_filters( 'hello-plus/template-parts/document/properties', $properties );
46 }
47
48 public function print_content(): void {
49 $plugin = Theme_Utils::elementor();
50
51 if ( $plugin->preview->is_preview_mode( $this->get_main_id() ) ) {
52 // PHPCS - this method is safe
53 echo $plugin->preview->builder_wrapper( '' ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
54 } else {
55 // PHPCS - this method is safe
56 echo $this->get_content(); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
57 }
58 }
59
60 public function get_css_wrapper_selector(): string {
61 return '.ehp-' . $this->get_main_id();
62 }
63
64 public static function get_create_url(): string {
65 $base_create_url = Theme_Utils::elementor()->documents->get_create_new_post_url( Source_Local::CPT );
66
67 return add_query_arg( [ 'template_type' => static::get_type() ], $base_create_url );
68 }
69
70 public function get_name(): string {
71 return static::get_type();
72 }
73
74 protected static function get_templates_path(): string {
75 return HELLOPLUS_PATH . '/modules/template-parts/templates/';
76 }
77
78 /**
79 * Retrieve the template-document post.
80 * There should be only one, so return null if not found, or found too many.
81 *
82 * @return ?int
83 */
84 public static function get_document_post(): ?int {
85 $posts = static::get_all_document_posts();
86
87 return ( 1 !== count( $posts ) ) ? null : $posts[0];
88 }
89
90 public static function get_all_document_posts( array $args = [] ): array {
91 $default_args = [
92 'post_type' => Source_Local::CPT,
93 'fields' => 'ids',
94 'lazy_load_term_meta' => true,
95 'tax_query' => [
96 [
97 'taxonomy' => static::TAXONOMY_TYPE_SLUG,
98 'field' => 'slug',
99 'terms' => static::get_type(),
100 ],
101 ],
102 ];
103
104 $args = wp_parse_args( $args, $default_args );
105
106 $query = new WP_Query( $args );
107
108 return $query->posts;
109 }
110
111 public static function get_active_document(): array {
112 return static::get_all_document_posts(
113 [
114 'post_status' => 'publish',
115 'posts_per_page' => 1,
116 ],
117 );
118 }
119
120 /**
121 * @return void
122 */
123 public static function register_hooks(): void {
124 $post = static::get_document_post();
125 if ( is_null( $post ) ) {
126 return;
127 }
128
129 if ( Theme_Utils::elementor()->preview->is_preview_mode() ) {
130 $post_id = filter_input( INPUT_GET, 'elementor-preview', FILTER_VALIDATE_INT );
131 $document = Theme_Utils::elementor()->documents->get( $post_id );
132
133 if ( $document instanceof Ehp_Document_Base ) {
134 return;
135 }
136 }
137
138 if ( Theme_Utils::is_preview_for_document( $post ) ) {
139 return;
140 }
141
142 $method_name = defined( 'ELEMENTOR_PRO_VERSION' ) ? 'maybe_get_template' : 'get_template';
143 add_action( static::get_template_hook(), [ static::get_class_full_name(), $method_name ], 10, 2 );
144 }
145
146 public static function maybe_get_template( ?string $name, array $args ): void {
147 if ( Utils::has_pro() ) {
148 /** @var $theme_builder_module */
149 $theme_builder_module = \ElementorPro\Modules\ThemeBuilder\Module::instance();
150 $conditions_manager = $theme_builder_module->get_conditions_manager();
151
152 $location_docs = $conditions_manager->get_documents_for_location( static::LOCATION );
153
154 if ( ! empty( $location_docs ) ) {
155 return;
156 }
157 }
158 static::get_template( $name, $args );
159 }
160
161 public function save( $data ) {
162 if ( empty( $data['settings']['template'] ) ) {
163 $data['settings']['template'] = Page_Templates_Module::TEMPLATE_CANVAS;
164 }
165
166 $active_documents = static::get_active_document();
167 if ( ! empty( $active_documents ) ) {
168 $data = $this->maybe_save_as_draft( $active_documents, $data );
169 }
170
171 return parent::save( $data );
172 }
173
174 protected function maybe_save_as_draft( $active_documents, $data ): array {
175 if ( ! empty( $data['settings']['post_status'] ) && 'publish' === $data['settings']['post_status'] ) {
176
177 $document_id = $this->get_main_id();
178
179 if ( ! in_array( $document_id, $active_documents, true ) ) {
180 $data['settings']['post_status'] = 'draft';
181 add_filter( 'elementor/documents/ajax_save/return_data', [ $this, 'allow_only_one_active_document' ], 10, 2 );
182 }
183 }
184
185 return $data;
186 }
187
188 /**
189 * @param $return_data
190 * @param $document
191 *
192 * @return mixed
193 * @throws \Exception
194 */
195 public function allow_only_one_active_document( $return_data, $document ) {
196 /* translators: %s: location (e.g. "header"). */
197 $message = sprintf( __( 'Your new %1$s has been saved as a "Draft". Activate this %1$s by navigating to Elementor > Saved Templates, and change your current %1$s\'s status to "Draft". Then publish your new %1$s.', 'hello-plus' ), static::LOCATION );
198 throw new \Exception( esc_html( $message ) );
199 }
200
201 protected function get_remote_library_config() {
202 $config = parent::get_remote_library_config();
203
204 $config['type'] = $this->get_name();
205 $config['default_route'] = 'templates/ehp-elements';
206 $config['autoImportSettings'] = true;
207
208 return $config;
209 }
210
211 /**
212 * The WP hook for rendering the relevant template.
213 *
214 * @return string
215 */
216 abstract public static function get_template_hook(): string;
217
218 /**
219 * @param ?string $name
220 * @param array $args
221 *
222 * @return mixed
223 */
224 abstract public static function get_template( ?string $name, array $args ): void;
225 }
226