action.php
4 years ago
api.php
4 years ago
base.php
4 years ago
hooks.php
4 years ago
templates.php
4 years ago
action.php
351 lines
| 1 | <?php |
| 2 | |
| 3 | namespace ShopEngine\Core\Builders; |
| 4 | |
| 5 | use ShopEngine\Core\Template_Cpt; |
| 6 | use ShopEngine\Traits\Singleton; |
| 7 | |
| 8 | defined('ABSPATH') || exit; |
| 9 | |
| 10 | class Action |
| 11 | { |
| 12 | use Singleton; |
| 13 | |
| 14 | const PK__SHOPENGINE_TEMPLATE = 'shopengine_template__post_meta'; |
| 15 | const EDIT_WITH_GUTENBERG = 'gutenberg'; |
| 16 | const EDIT_WITH_ELEMENTOR = 'elementor'; |
| 17 | |
| 18 | /** |
| 19 | * @var mixed |
| 20 | */ |
| 21 | private $form_id; |
| 22 | /** |
| 23 | * @var mixed |
| 24 | */ |
| 25 | private static $form_settings; |
| 26 | /** |
| 27 | * @var string |
| 28 | */ |
| 29 | private static $edit_with = ''; |
| 30 | |
| 31 | /** |
| 32 | * @param $form_id |
| 33 | * @param $form_settings |
| 34 | * @return mixed |
| 35 | */ |
| 36 | public function store($form_id, $form_settings) |
| 37 | { |
| 38 | $this->set_form_values($form_settings); |
| 39 | $this->form_id = $form_id; |
| 40 | |
| 41 | if ($this->form_id == 0) { |
| 42 | global $wp_rewrite; |
| 43 | $wp_rewrite->flush_rules(); |
| 44 | $wp_rewrite->init(); |
| 45 | |
| 46 | return $this->insert(); |
| 47 | } else { |
| 48 | return $this->update(); |
| 49 | } |
| 50 | } |
| 51 | |
| 52 | public function insert() |
| 53 | { |
| 54 | $form_settings = self::$form_settings; |
| 55 | |
| 56 | $title = ($form_settings['form_title'] != '') ? $form_settings['form_title'] : 'New Template # ' . time(); |
| 57 | |
| 58 | $form_id = wp_insert_post([ |
| 59 | 'post_title' => $title, |
| 60 | 'post_status' => 'publish', |
| 61 | 'post_type' => Template_Cpt::TYPE |
| 62 | ]); |
| 63 | |
| 64 | /** |
| 65 | * Get template default meta |
| 66 | */ |
| 67 | $edit_with = self::get_form_value('edit_with_option', self::EDIT_WITH_GUTENBERG); |
| 68 | $default = self::get_form_value('set_default', 'No'); |
| 69 | $template_type = self::get_form_value('form_type', 'single'); |
| 70 | $category_id = !empty($form_settings['category_id']) ? '__' . $form_settings['category_id'] : ''; |
| 71 | |
| 72 | /** |
| 73 | * Set template default meta |
| 74 | */ |
| 75 | update_post_meta($form_id, self::get_meta_key_for_type(), $template_type); |
| 76 | update_post_meta($form_id, self::PK__SHOPENGINE_TEMPLATE, $form_settings); |
| 77 | update_post_meta($form_id, self::get_meta_key_for_edit_with(), $edit_with); |
| 78 | |
| 79 | $this->conditional_template($template_type); |
| 80 | /** |
| 81 | * Template active status checking |
| 82 | */ |
| 83 | if ($default == 'Yes') { |
| 84 | /** |
| 85 | * Generate created template type key |
| 86 | */ |
| 87 | $template_key_type = self::PK__SHOPENGINE_TEMPLATE . '__' . $template_type . $category_id; |
| 88 | |
| 89 | |
| 90 | update_option($template_key_type, $form_id); |
| 91 | } |
| 92 | |
| 93 | /** |
| 94 | * If this template is dependent on elementor page builder then this meta will be saved |
| 95 | */ |
| 96 | if ($edit_with === self::EDIT_WITH_ELEMENTOR) { |
| 97 | /** |
| 98 | * Auto elementor canvas style |
| 99 | */ |
| 100 | if (in_array($template_type, ['quick_checkout', 'quick_view'])) { |
| 101 | update_post_meta($form_id, '_wp_page_template', 'elementor_canvas'); |
| 102 | } else { |
| 103 | update_post_meta($form_id, '_wp_page_template', 'elementor_header_footer'); |
| 104 | } |
| 105 | update_post_meta($form_id, '_elementor_edit_mode', 'builder'); |
| 106 | update_post_meta($form_id, '_elementor_version', '3.4.6'); |
| 107 | } |
| 108 | |
| 109 | if (!empty($form_settings['sample_design'])) { |
| 110 | |
| 111 | /** |
| 112 | * Get ready made template data |
| 113 | */ |
| 114 | $design_data = \ShopEngine\Core\Sample_Designs\Base::instance()->get_design_data($form_settings['sample_design']); |
| 115 | |
| 116 | if (!is_null($design_data)) { |
| 117 | /** |
| 118 | * for unicode character support |
| 119 | */ |
| 120 | $design_data = wp_slash(wp_json_encode($design_data)); |
| 121 | |
| 122 | update_post_meta($form_id, '_elementor_data', $design_data); |
| 123 | } |
| 124 | } |
| 125 | |
| 126 | return [ |
| 127 | 'saved' => true, |
| 128 | 'data' => [ |
| 129 | 'id' => $form_id, |
| 130 | 'title' => $title, |
| 131 | 'type' => Template_Cpt::TYPE |
| 132 | ], |
| 133 | 'status' => esc_html__('Template settings inserted', 'shopengine') |
| 134 | ]; |
| 135 | } |
| 136 | |
| 137 | public function update() |
| 138 | { |
| 139 | $form_settings = self::$form_settings; |
| 140 | |
| 141 | $title = ($form_settings['form_title'] != '') ? $form_settings['form_title'] : 'New Template # ' . time(); |
| 142 | |
| 143 | wp_update_post([ |
| 144 | 'ID' => $this->form_id, |
| 145 | 'post_title' => $title, |
| 146 | 'post_status' => 'publish' |
| 147 | ]); |
| 148 | |
| 149 | /** |
| 150 | * Get template default meta |
| 151 | */ |
| 152 | $default = self::get_form_value('set_default', 'No'); |
| 153 | $template_type = self::get_form_value('form_type', 'single'); |
| 154 | $category_id = !empty($form_settings['category_id']) ? '__' . $form_settings['category_id'] : ''; |
| 155 | |
| 156 | $form_settings['set_default'] = $default; |
| 157 | |
| 158 | $old_form_settings = get_post_meta($this->form_id, self::PK__SHOPENGINE_TEMPLATE, true); |
| 159 | update_post_meta($this->form_id, self::PK__SHOPENGINE_TEMPLATE, $form_settings); |
| 160 | update_post_meta($this->form_id, self::get_meta_key_for_type(), $template_type); |
| 161 | |
| 162 | /** |
| 163 | * Generate created template type key |
| 164 | */ |
| 165 | $template_key_type = self::PK__SHOPENGINE_TEMPLATE . '__' . $template_type . $category_id; |
| 166 | |
| 167 | $this->conditional_template($template_type, $old_form_settings); |
| 168 | /** |
| 169 | * Template active status checking |
| 170 | */ |
| 171 | if ($default == 'Yes') { |
| 172 | update_option($template_key_type, $this->form_id); |
| 173 | |
| 174 | } elseif ($default == 'No') { |
| 175 | update_option($template_key_type, 0); |
| 176 | } |
| 177 | |
| 178 | |
| 179 | return [ |
| 180 | 'saved' => true, |
| 181 | 'data' => [ |
| 182 | 'id' => $this->form_id, |
| 183 | 'title' => $title, |
| 184 | 'type' => Template_Cpt::TYPE |
| 185 | ], |
| 186 | 'status' => esc_html__('Template settings updated', 'shopengine') |
| 187 | ]; |
| 188 | } |
| 189 | |
| 190 | /** |
| 191 | * @param $template_type |
| 192 | * @param array $old_form_settings |
| 193 | */ |
| 194 | public function conditional_template($template_type, $old_form_settings = []) |
| 195 | { |
| 196 | |
| 197 | $form_settings = self::$form_settings; |
| 198 | |
| 199 | if ($template_type === 'single' || $template_type === 'archive') { |
| 200 | |
| 201 | if (!empty($form_settings['category_id']) && !empty($old_form_settings['category_id'])) { // category to category |
| 202 | |
| 203 | if ($old_form_settings['category_id'] != $form_settings['category_id']) { |
| 204 | |
| 205 | $old_template_key_type = self::PK__SHOPENGINE_TEMPLATE . '__' . $template_type . '__' . $old_form_settings['category_id']; |
| 206 | |
| 207 | $old_template_id = get_option($old_template_key_type); |
| 208 | |
| 209 | if ($old_template_id == $this->form_id) { |
| 210 | update_option($old_template_key_type, 0); |
| 211 | } |
| 212 | } |
| 213 | |
| 214 | } elseif (!empty($old_form_settings['category_id'])) { // category to normal |
| 215 | |
| 216 | $old_template_key_type = self::PK__SHOPENGINE_TEMPLATE . '__' . $template_type . '__' . $old_form_settings['category_id']; |
| 217 | |
| 218 | $old_template_id = get_option($old_template_key_type); |
| 219 | |
| 220 | if ($old_template_id == $this->form_id) { |
| 221 | update_option($old_template_key_type, 0); |
| 222 | } |
| 223 | |
| 224 | } else { // normal to category |
| 225 | |
| 226 | $template_id_with_out_category = get_option(self::PK__SHOPENGINE_TEMPLATE . '__' . $template_type); |
| 227 | |
| 228 | if ($this->form_id == $template_id_with_out_category) { |
| 229 | update_option(self::PK__SHOPENGINE_TEMPLATE . '__' . $template_type, 0); |
| 230 | } |
| 231 | } |
| 232 | } |
| 233 | } |
| 234 | |
| 235 | /** |
| 236 | * @param $post_id |
| 237 | * @return mixed |
| 238 | */ |
| 239 | public function get_all_data($post_id) |
| 240 | { |
| 241 | $post = get_post($post_id); |
| 242 | $data = get_post_meta($post->ID, self::PK__SHOPENGINE_TEMPLATE, true); |
| 243 | $type = get_post_meta($post->ID, self::get_meta_key_for_type(), true); |
| 244 | $data['form_title'] = get_the_title($post_id); |
| 245 | $data['set_default'] = 'No'; |
| 246 | $data['edit_with_option'] = get_post_meta($post->ID, self::get_meta_key_for_edit_with(), true); |
| 247 | |
| 248 | if (!empty($data['category_id'])) { |
| 249 | |
| 250 | $category = '__' . $data['category_id']; |
| 251 | $saved_id = get_option(Action::PK__SHOPENGINE_TEMPLATE . '__' . $type . $category, 0); |
| 252 | |
| 253 | if ($saved_id == $post->ID) { |
| 254 | $data['set_default'] = 'Yes'; |
| 255 | } |
| 256 | |
| 257 | } else { |
| 258 | if (Templates::get_registered_template_id($type) == $post->ID) { |
| 259 | $data['set_default'] = 'Yes'; |
| 260 | } |
| 261 | } |
| 262 | return $data; |
| 263 | } |
| 264 | |
| 265 | public static function get_meta_key_for_type() |
| 266 | { |
| 267 | return self::PK__SHOPENGINE_TEMPLATE . '__type'; |
| 268 | } |
| 269 | |
| 270 | public static function get_meta_key_for_edit_with() |
| 271 | { |
| 272 | return self::PK__SHOPENGINE_TEMPLATE . '__edit_with'; |
| 273 | } |
| 274 | |
| 275 | /** |
| 276 | * @param $template_id |
| 277 | */ |
| 278 | public static function edit_with($template_id) |
| 279 | { |
| 280 | if (static::$edit_with) { |
| 281 | return static::$edit_with; |
| 282 | } |
| 283 | |
| 284 | $edit_with = get_post_meta($template_id, Action::get_meta_key_for_edit_with(), true); |
| 285 | static::$edit_with = empty($edit_with) ? Action::EDIT_WITH_ELEMENTOR : $edit_with; |
| 286 | |
| 287 | return static::$edit_with; |
| 288 | } |
| 289 | |
| 290 | /** |
| 291 | * @param $pid |
| 292 | * @return mixed |
| 293 | */ |
| 294 | public static function is_edit_with_gutenberg($pid) |
| 295 | { |
| 296 | $edit_with = get_post_meta($pid, Action::get_meta_key_for_edit_with(), true); |
| 297 | $edit_with = empty($edit_with) ? Action::EDIT_WITH_ELEMENTOR : $edit_with; |
| 298 | |
| 299 | return $edit_with === self::EDIT_WITH_GUTENBERG; |
| 300 | } |
| 301 | |
| 302 | /** |
| 303 | * @param $field |
| 304 | * @param $default |
| 305 | */ |
| 306 | private static function get_form_value($field, $default = '') |
| 307 | { |
| 308 | return isset(self::$form_settings[$field]) ? self::$form_settings[$field] : $default; |
| 309 | } |
| 310 | |
| 311 | /** |
| 312 | * @param $form_settings |
| 313 | * @param $fields |
| 314 | */ |
| 315 | private function set_form_values($form_settings, $fields = null) |
| 316 | { |
| 317 | $fields = $this->get_fields(); |
| 318 | |
| 319 | foreach ($form_settings as $key => $value) { |
| 320 | |
| 321 | if (isset($fields[$key])) { |
| 322 | self::$form_settings[$key] = $value; |
| 323 | } |
| 324 | } |
| 325 | } |
| 326 | |
| 327 | public function get_fields() |
| 328 | { |
| 329 | return [ |
| 330 | 'form_title' => [ |
| 331 | 'name' => 'form_title' |
| 332 | ], |
| 333 | 'form_type' => [ |
| 334 | 'name' => 'form_type' |
| 335 | ], |
| 336 | 'set_default' => [ |
| 337 | 'name' => 'set_default' |
| 338 | ], |
| 339 | 'edit_with_option' => [ |
| 340 | 'name' => 'edit_with_option' |
| 341 | ], |
| 342 | 'sample_design' => [ |
| 343 | 'name' => 'sample_design' |
| 344 | ], |
| 345 | 'category_id' => [ |
| 346 | 'name' => 'category_id' |
| 347 | ] |
| 348 | ]; |
| 349 | } |
| 350 | } |
| 351 |