action.php
5 years ago
api.php
5 years ago
base.php
5 years ago
hooks.php
5 years ago
templates.php
5 years ago
action.php
233 lines
| 1 | <?php |
| 2 | |
| 3 | namespace ShopEngine\Core\Builders; |
| 4 | |
| 5 | defined('ABSPATH') || exit; |
| 6 | |
| 7 | use ShopEngine\Core\Template_Cpt; |
| 8 | use ShopEngine\Traits\Singleton; |
| 9 | |
| 10 | /** |
| 11 | * Action Class. |
| 12 | * for post insert, update and get data. |
| 13 | * |
| 14 | * @since 1.0.0 |
| 15 | */ |
| 16 | class Action { |
| 17 | |
| 18 | use Singleton; |
| 19 | |
| 20 | const PK__SHOPENGINE_TEMPLATE = 'shopengine_template__post_meta'; |
| 21 | |
| 22 | public $key_form_settings; |
| 23 | private $post_type; |
| 24 | |
| 25 | private $fields; |
| 26 | private $form_id; |
| 27 | private $form_setting; |
| 28 | private $title; |
| 29 | private $response = []; |
| 30 | |
| 31 | |
| 32 | /** |
| 33 | * Public function __construct. |
| 34 | * call function for all |
| 35 | * |
| 36 | * @since 1.0.0 |
| 37 | */ |
| 38 | public function __construct() { |
| 39 | |
| 40 | $this->post_type = Template_Cpt::TYPE; |
| 41 | |
| 42 | $this->key_form_settings = self::PK__SHOPENGINE_TEMPLATE; |
| 43 | |
| 44 | $this->response = [ |
| 45 | 'saved' => false, |
| 46 | 'status' => esc_html__("Something went wrong.", 'shopengine'), |
| 47 | 'data' => [], |
| 48 | ]; |
| 49 | } |
| 50 | |
| 51 | |
| 52 | /** |
| 53 | * Public function store. |
| 54 | * store data for post |
| 55 | * |
| 56 | * @since 1.0.0 |
| 57 | */ |
| 58 | public function store($form_id, $form_setting) { |
| 59 | |
| 60 | $this->fields = $this->get_fields(); |
| 61 | $this->set_values($form_setting); |
| 62 | $this->form_id = $form_id; |
| 63 | |
| 64 | if($this->form_id == 0) { |
| 65 | $this->insert(); |
| 66 | } else { |
| 67 | $this->update(); |
| 68 | } |
| 69 | |
| 70 | return $this->response; |
| 71 | } |
| 72 | |
| 73 | |
| 74 | public function insert() { |
| 75 | |
| 76 | $this->title = ($this->form_setting['form_title'] != '') ? $this->form_setting['form_title'] : 'New Template # ' . time(); |
| 77 | |
| 78 | $defaults = array( |
| 79 | 'post_title' => $this->title, |
| 80 | 'post_status' => 'publish', |
| 81 | 'post_type' => $this->post_type, |
| 82 | ); |
| 83 | |
| 84 | $this->form_id = wp_insert_post($defaults); |
| 85 | |
| 86 | // save custom meta data |
| 87 | update_post_meta($this->form_id, self::PK__SHOPENGINE_TEMPLATE, $this->form_setting); |
| 88 | |
| 89 | |
| 90 | |
| 91 | //set default meta |
| 92 | $default = isset($this->form_setting['set_defalut']) ? $this->form_setting['set_defalut'] : 'No'; |
| 93 | $type = isset($this->form_setting['form_type']) ? $this->form_setting['form_type'] : 'single'; |
| 94 | |
| 95 | update_post_meta($this->form_id, self::PK__SHOPENGINE_TEMPLATE . '__type', $type); |
| 96 | |
| 97 | // check options key value |
| 98 | $keyType = self::PK__SHOPENGINE_TEMPLATE . '__' . $type; |
| 99 | |
| 100 | if($default == 'Yes') { |
| 101 | update_option($keyType, $this->form_id); |
| 102 | } |
| 103 | |
| 104 | // auto elementor canvas style |
| 105 | update_post_meta($this->form_id, '_wp_page_template', 'elementor_canvas'); |
| 106 | update_post_meta( $this->form_id, '_elementor_edit_mode', 'builder'); |
| 107 | |
| 108 | if(!empty($this->form_setting['sample_design'])){ |
| 109 | $design_data = \ShopEngine\Core\Sample_Designs\Base::instance()->get_design_data($this->form_setting['sample_design']); |
| 110 | if(!is_null($design_data)){ |
| 111 | update_post_meta($this->form_id, '_elementor_data', json_encode($design_data)); |
| 112 | } |
| 113 | } |
| 114 | |
| 115 | $this->response['saved'] = true; |
| 116 | $this->response['status'] = esc_html__('Template settings inserted', 'shopengine'); |
| 117 | |
| 118 | $this->response['data']['id'] = $this->form_id; |
| 119 | $this->response['data']['title'] = $this->title; |
| 120 | $this->response['data']['type'] = $this->post_type; |
| 121 | } |
| 122 | |
| 123 | |
| 124 | public function update() { |
| 125 | |
| 126 | $this->title = ($this->form_setting['form_title'] != '') ? $this->form_setting['form_title'] : 'New Template # ' . time(); |
| 127 | |
| 128 | if(isset($this->form_setting['form_title'])) { |
| 129 | $update_post = array( |
| 130 | 'ID' => $this->form_id, |
| 131 | 'post_title' => $this->title, |
| 132 | ); |
| 133 | wp_update_post($update_post); |
| 134 | } |
| 135 | |
| 136 | // save custom meta data |
| 137 | update_post_meta($this->form_id, $this->key_form_settings, $this->form_setting); |
| 138 | |
| 139 | //set default meta |
| 140 | $default = isset($this->form_setting['set_defalut']) ? $this->form_setting['set_defalut'] : 'No'; |
| 141 | $type = isset($this->form_setting['form_type']) ? $this->form_setting['form_type'] : 'single'; |
| 142 | |
| 143 | update_post_meta($this->form_id, $this->key_form_settings . '__type', $type); |
| 144 | |
| 145 | // check options key value |
| 146 | $keyType = $this->key_form_settings . '__' . $type; |
| 147 | |
| 148 | $getSetDefault = get_option($keyType, 0); |
| 149 | if($default == 'Yes') { |
| 150 | update_option($keyType, $this->form_id); |
| 151 | } elseif($this->form_id == $getSetDefault) { |
| 152 | update_option($keyType, 0); |
| 153 | } |
| 154 | |
| 155 | //auto elementor canvas style |
| 156 | update_post_meta($this->form_id, '_wp_page_template', 'elementor_canvas'); |
| 157 | |
| 158 | $this->response['saved'] = true; |
| 159 | $this->response['status'] = esc_html__('Template settings updated', 'shopengine'); |
| 160 | |
| 161 | $this->response['data']['id'] = $this->form_id; |
| 162 | $this->response['data']['title'] = $this->title; |
| 163 | $this->response['data']['type'] = $this->post_type; |
| 164 | } |
| 165 | |
| 166 | |
| 167 | /** |
| 168 | * |
| 169 | * @return array |
| 170 | */ |
| 171 | public function get_fields() { |
| 172 | |
| 173 | return [ |
| 174 | |
| 175 | 'form_title' => [ |
| 176 | 'name' => 'form_title', |
| 177 | ], |
| 178 | 'form_type' => [ |
| 179 | 'name' => 'form_type', |
| 180 | ], |
| 181 | 'set_defalut' => [ |
| 182 | 'name' => 'set_defalut', |
| 183 | ], |
| 184 | 'sample_design' => [ |
| 185 | 'name' => 'sample_design', |
| 186 | ], |
| 187 | ]; |
| 188 | } |
| 189 | |
| 190 | |
| 191 | /** |
| 192 | * |
| 193 | * @param $form_setting |
| 194 | * @param null $fields |
| 195 | */ |
| 196 | public function set_values($form_setting, $fields = null) { |
| 197 | |
| 198 | if($fields == null) { |
| 199 | $fields = $this->fields; |
| 200 | } |
| 201 | |
| 202 | foreach($form_setting as $key => $value) { |
| 203 | |
| 204 | if(isset($fields[$key])) { |
| 205 | $this->form_setting[$key] = $value; |
| 206 | } |
| 207 | } |
| 208 | } |
| 209 | |
| 210 | |
| 211 | /** |
| 212 | * |
| 213 | * @param $post_id |
| 214 | * |
| 215 | * @return mixed |
| 216 | */ |
| 217 | public function get_all_data($post_id) { |
| 218 | |
| 219 | $post = get_post($post_id); |
| 220 | $data = get_post_meta($post->ID, $this->key_form_settings, true); |
| 221 | $type = isset($data['form_type']) ? $data['form_type'] : 'single'; |
| 222 | |
| 223 | |
| 224 | $data['form_title'] = get_the_title($post_id); |
| 225 | $data['set_defalut'] = 'No'; |
| 226 | |
| 227 | if(Templates::get_default_template_id_for_type($type) == $post->ID) { |
| 228 | $data['set_defalut'] = 'Yes'; |
| 229 | } |
| 230 | |
| 231 | return $data; |
| 232 | } |
| 233 | } |