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
hooks.php
288 lines
| 1 | <?php |
| 2 | |
| 3 | namespace ShopEngine\Core\Builders; |
| 4 | |
| 5 | use ShopEngine\Core\PageTemplates\Page_Templates; |
| 6 | use ShopEngine\Core\Template_Cpt; |
| 7 | use ShopEngine\Traits\Singleton; |
| 8 | use ShopEngine\Utils\Helper; |
| 9 | |
| 10 | defined('ABSPATH') || exit; |
| 11 | |
| 12 | class Hooks { |
| 13 | |
| 14 | use Singleton; |
| 15 | |
| 16 | public $action; |
| 17 | public $actionPost_type = ['product']; // only for woocommerce product |
| 18 | |
| 19 | |
| 20 | public function init() { |
| 21 | |
| 22 | $this->action = new Action(); |
| 23 | $cptName = Template_Cpt::TYPE; |
| 24 | |
| 25 | // check admin init |
| 26 | add_action('admin_init', [$this, 'add_author_support'], 10); |
| 27 | add_filter('manage_' . $cptName . '_posts_columns', [$this, 'set_columns']); |
| 28 | add_action('manage_' . $cptName . '_posts_custom_column', [$this, 'render_column'], 10, 2); |
| 29 | |
| 30 | // add filter for search |
| 31 | add_action('restrict_manage_posts', [$this, 'add_filter']); |
| 32 | // query filter |
| 33 | add_filter('parse_query', [$this, 'query_filter']); |
| 34 | } |
| 35 | |
| 36 | |
| 37 | /** |
| 38 | * Public function add_author_support. |
| 39 | * check author support |
| 40 | * |
| 41 | * @since 1.0.0 |
| 42 | */ |
| 43 | public function add_author_support() { |
| 44 | |
| 45 | add_post_type_support(Template_Cpt::TYPE, 'author'); |
| 46 | } |
| 47 | |
| 48 | |
| 49 | /** |
| 50 | * Public function set_columns. |
| 51 | * set column for custom post type |
| 52 | * |
| 53 | * @since 1.0.0 |
| 54 | */ |
| 55 | public function set_columns($columns) { |
| 56 | |
| 57 | $date_column = $columns['date']; |
| 58 | $author_column = $columns['author']; |
| 59 | |
| 60 | unset($columns['date']); |
| 61 | unset($columns['author']); |
| 62 | |
| 63 | $columns['type'] = esc_html__('Type', 'shopengine'); |
| 64 | $columns['set_default'] = esc_html__('Default', 'shopengine'); |
| 65 | $columns['builder'] = esc_html__('Builder', 'shopengine'); |
| 66 | $columns['author'] = esc_html($author_column); |
| 67 | $columns['date'] = esc_html($date_column); |
| 68 | |
| 69 | return $columns; |
| 70 | } |
| 71 | |
| 72 | |
| 73 | /** |
| 74 | * Public function render_column. |
| 75 | * Render column for custom post type |
| 76 | * |
| 77 | * @param $column |
| 78 | * @param $post_id |
| 79 | * @since 1.0.0 |
| 80 | * |
| 81 | */ |
| 82 | public function render_column($column, $post_id) { |
| 83 | |
| 84 | $data = get_post_meta($post_id, Action::PK__SHOPENGINE_TEMPLATE, true); |
| 85 | $template_type = isset($data['form_type']) ? $data['form_type'] : ''; |
| 86 | $template_config_data = \ShopEngine\Core\PageTemplates\Page_Templates::instance()->getTemplate($template_type); |
| 87 | $template_class = $template_config_data['class'] ?? null; |
| 88 | $category = isset($data['category_id']) ? $data['category_id'] : ''; |
| 89 | |
| 90 | if('' !== $category) { |
| 91 | $template_id = get_option(Action::PK__SHOPENGINE_TEMPLATE . '__' . $template_type.'__'.$category, 0); |
| 92 | } else { |
| 93 | $template_id = Templates::get_registered_template_id($template_type); |
| 94 | } |
| 95 | |
| 96 | switch($column) { |
| 97 | case 'type': |
| 98 | |
| 99 | $template = Page_Templates::instance()->getTemplate($template_type); |
| 100 | echo empty($template) ? '' : $template['title']; |
| 101 | if(isset($data['category_id']) && class_exists(\ShopEngine_Pro::class)) { |
| 102 | $cat_name = get_the_category_by_ID($data['category_id']); |
| 103 | |
| 104 | if(isset($cat_name) && !is_wp_error($cat_name)) { |
| 105 | echo '<br>' . esc_html__('Category', 'shopengine') .' : '. esc_html($cat_name); |
| 106 | } |
| 107 | } |
| 108 | break; |
| 109 | |
| 110 | case 'builder': |
| 111 | $builder = Helper::get_template_builder_type($post_id);; |
| 112 | echo empty($builder) ? 'elementor' : $builder; |
| 113 | break; |
| 114 | |
| 115 | case 'set_default': |
| 116 | |
| 117 | if($template_id == $post_id && class_exists($template_class)) { |
| 118 | echo '<span class="shopengine_default cat__'.$category.' type-'.esc_attr($template_type).' shopengine-active"> ' . esc_html__('Active', 'shopengine') . ' </span>'; |
| 119 | } else { |
| 120 | echo '<span class="shopengine_default cat__'.$category.' type-'.esc_attr($template_type).' shopengine-deactive"> ' . esc_html__('Inactive', 'shopengine') . ' </span>'; |
| 121 | } |
| 122 | break; |
| 123 | } |
| 124 | } |
| 125 | |
| 126 | |
| 127 | /** |
| 128 | * Public function add_filter. |
| 129 | * Added search filter for type of template |
| 130 | * |
| 131 | * @since 1.0.0 |
| 132 | */ |
| 133 | public function add_filter() { |
| 134 | |
| 135 | global $typenow; |
| 136 | |
| 137 | if($typenow == Template_Cpt::TYPE) { |
| 138 | |
| 139 | $selected = isset($_GET['type']) ? sanitize_key($_GET['type']) : ''; ?> |
| 140 | |
| 141 | <select name="type" id="type"> |
| 142 | |
| 143 | <option value="all" <?php selected('all', $selected); ?>><?php esc_html_e('Template Type ', 'shopengine'); ?></option> <?php |
| 144 | |
| 145 | foreach(Templates::get_template_types() as $key => $value) { ?> |
| 146 | <option value="<?php echo esc_attr($key); ?>" <?php selected($key, $selected); ?>><?php esc_html_e($value['title'], 'shopengine'); ?></option> |
| 147 | <?php } ?> |
| 148 | |
| 149 | </select> |
| 150 | <?php |
| 151 | } |
| 152 | } |
| 153 | |
| 154 | |
| 155 | /** |
| 156 | * Public function query_filter. |
| 157 | * Search query filter added in search query |
| 158 | * |
| 159 | * @param $query |
| 160 | * @since 1.0.0 |
| 161 | */ |
| 162 | public function query_filter($query) { |
| 163 | |
| 164 | global $pagenow; |
| 165 | |
| 166 | $current_page = isset($_GET['post_type']) ? sanitize_key($_GET['post_type']) : ''; |
| 167 | |
| 168 | if( |
| 169 | is_admin() |
| 170 | && Template_Cpt::TYPE == $current_page |
| 171 | && 'edit.php' == $pagenow |
| 172 | && isset($_GET['type']) |
| 173 | && $_GET['type'] != '' |
| 174 | && $_GET['type'] != 'all' |
| 175 | ) { |
| 176 | $type = isset($_GET['type']) ? sanitize_key($_GET['type']) : ''; |
| 177 | $query->query_vars['meta_key'] = Action::get_meta_key_for_type(); |
| 178 | $query->query_vars['meta_value'] = $type; |
| 179 | $query->query_vars['meta_compare'] = '='; |
| 180 | } |
| 181 | } |
| 182 | |
| 183 | |
| 184 | /** |
| 185 | * Public function template_selected. |
| 186 | * add meta box for choose template ShopEngine |
| 187 | * |
| 188 | * @since 1.0.0 |
| 189 | */ |
| 190 | public function template_selected() { |
| 191 | global $post; |
| 192 | |
| 193 | if(in_array($post->post_type, $this->actionPost_type)): |
| 194 | foreach($this->actionPost_type as $k => $v): |
| 195 | add_meta_box( |
| 196 | 'shopengine_template', |
| 197 | esc_html__('ShopEngine Template', 'shopengine'), |
| 198 | [$this, 'shopengine_template'], |
| 199 | $v, |
| 200 | 'side', |
| 201 | 'low' |
| 202 | ); |
| 203 | endforeach; |
| 204 | endif; |
| 205 | } |
| 206 | |
| 207 | |
| 208 | /** |
| 209 | * Public function template_save. |
| 210 | * ShopEngine Template save for product |
| 211 | * |
| 212 | * @since 1.0.0 |
| 213 | */ |
| 214 | public function template_save($post_id, $post) { |
| 215 | if(!current_user_can('edit_post', $post_id)) { |
| 216 | return $post_id; |
| 217 | } |
| 218 | |
| 219 | if(in_array($post->post_type, $this->actionPost_type)): |
| 220 | if(isset($_POST['shopengine-template'])) { |
| 221 | update_post_meta($post_id, Action::PK__SHOPENGINE_TEMPLATE . '__template', sanitize_key($_POST['shopengine-template'])); |
| 222 | } |
| 223 | endif; |
| 224 | } |
| 225 | |
| 226 | |
| 227 | /** |
| 228 | * Public function shopengine_template. |
| 229 | * ShopEngine Template Html |
| 230 | * |
| 231 | * @since 1.0.0 |
| 232 | */ |
| 233 | public function shopengine_template() { |
| 234 | global $post; |
| 235 | |
| 236 | if(!isset($post->ID)) { |
| 237 | return ''; |
| 238 | } |
| 239 | |
| 240 | $page_template = get_post_meta($post->ID, Action::PK__SHOPENGINE_TEMPLATE . '__template', true); |
| 241 | |
| 242 | $template = $this->get_post_single(); |
| 243 | echo '<select name="shopengine-template">'; |
| 244 | echo '<option value="0"> ' . esc_html__('Default', 'shopengine') . ' </option>'; |
| 245 | if(is_array($template) && sizeof($template) > 0) { |
| 246 | foreach($template as $k => $v) { |
| 247 | $select = ''; |
| 248 | if($page_template == $k) { |
| 249 | $select = 'selected'; |
| 250 | } |
| 251 | echo '<option value="' . $k . '" ' . $select . '> ' . esc_html($v, 'shopengine') . ' </option>'; |
| 252 | } |
| 253 | } |
| 254 | echo '</select>'; |
| 255 | } |
| 256 | |
| 257 | |
| 258 | /** |
| 259 | * get query post query |
| 260 | * |
| 261 | * @return array |
| 262 | */ |
| 263 | public function get_post_single() { |
| 264 | |
| 265 | $args['post_status'] = 'publish'; |
| 266 | $args['post_type'] = Template_Cpt::TYPE; |
| 267 | $args['meta_query'] = [ |
| 268 | 'relation' => 'AND', |
| 269 | [ |
| 270 | 'key' => Action::get_meta_key_for_type(), |
| 271 | 'value' => 'single', |
| 272 | 'compare' => '=', |
| 273 | ], |
| 274 | ]; |
| 275 | |
| 276 | $posts = get_posts($args); |
| 277 | $options = []; |
| 278 | $count = count($posts); |
| 279 | if($count > 0): |
| 280 | foreach($posts as $post) { |
| 281 | $options[$post->ID] = $post->post_title; |
| 282 | } |
| 283 | endif; |
| 284 | |
| 285 | return $options; |
| 286 | } |
| 287 | } |
| 288 |