action.php
4 years ago
api.php
5 years ago
base.php
4 years ago
hooks.php
4 years ago
templates.php
4 years ago
hooks.php
275 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 | |
| 9 | defined('ABSPATH') || exit; |
| 10 | |
| 11 | class Hooks |
| 12 | { |
| 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['author'] = esc_html($author_column); |
| 66 | $columns['date'] = esc_html($date_column); |
| 67 | |
| 68 | return $columns; |
| 69 | } |
| 70 | |
| 71 | |
| 72 | /** |
| 73 | * Public function render_column. |
| 74 | * Render column for custom post type |
| 75 | * |
| 76 | * @since 1.0.0 |
| 77 | * |
| 78 | * @param $column |
| 79 | * @param $post_id |
| 80 | */ |
| 81 | public function render_column($column, $post_id) { |
| 82 | |
| 83 | $data = get_post_meta($post_id, Action::PK__SHOPENGINE_TEMPLATE, true); |
| 84 | $template_type = isset($data['form_type']) ? $data['form_type'] : ''; |
| 85 | $get_id_from_db_by_template_type = Templates::get_registered_template_id($template_type); |
| 86 | $template_config_data = \ShopEngine\Core\PageTemplates\Page_Templates::instance()->getTemplate($template_type); |
| 87 | $template_class = $template_config_data['class'] ?? null; |
| 88 | |
| 89 | switch($column) { |
| 90 | case 'type': |
| 91 | // echo '<pre>'; |
| 92 | // print_r([$template_type, $get_id_from_db_by_template_type, $template_config_data, $template_class]); |
| 93 | // echo '</pre>'; |
| 94 | |
| 95 | $template = Page_Templates::instance()->getTemplate($template_type); |
| 96 | echo empty($template) ? '' : $template['title']; |
| 97 | break; |
| 98 | |
| 99 | case 'set_default': |
| 100 | |
| 101 | |
| 102 | if($get_id_from_db_by_template_type == $post_id && class_exists($template_class)) { |
| 103 | echo '<span class="shopengine_default met-active"> ' . esc_html__('Active', 'shopengine') . ' </span>'; |
| 104 | } else { |
| 105 | echo '<span class="shopengine_default met-deactive"> ' . esc_html__('Inactive', 'shopengine') . ' </span>'; |
| 106 | } |
| 107 | break; |
| 108 | } |
| 109 | } |
| 110 | |
| 111 | |
| 112 | /** |
| 113 | * Public function add_filter. |
| 114 | * Added search filter for type of template |
| 115 | * |
| 116 | * @since 1.0.0 |
| 117 | */ |
| 118 | public function add_filter() { |
| 119 | |
| 120 | global $typenow; |
| 121 | |
| 122 | if($typenow == Template_Cpt::TYPE) { |
| 123 | |
| 124 | $selected = isset($_GET['type']) ? sanitize_key($_GET['type']) : ''; ?> |
| 125 | |
| 126 | <select name="type" id="type"> |
| 127 | |
| 128 | <option value="all" <?php selected('all', $selected); ?>><?php esc_html_e('Template Type ', 'shopengine'); ?></option> <?php |
| 129 | |
| 130 | foreach(Templates::get_template_types() as $key => $value) { ?> |
| 131 | <option value="<?php echo esc_attr($key); ?>" <?php selected($key, $selected); ?>><?php echo esc_attr($value['title']); ?></option> <?php |
| 132 | } |
| 133 | |
| 134 | ?> |
| 135 | |
| 136 | </select> |
| 137 | <?php |
| 138 | } |
| 139 | } |
| 140 | |
| 141 | |
| 142 | /** |
| 143 | * Public function query_filter. |
| 144 | * Search query filter added in search query |
| 145 | * |
| 146 | * @since 1.0.0 |
| 147 | * @param $query |
| 148 | */ |
| 149 | public function query_filter($query) { |
| 150 | |
| 151 | global $pagenow; |
| 152 | |
| 153 | $current_page = isset($_GET['post_type']) ? sanitize_key($_GET['post_type']) : ''; |
| 154 | |
| 155 | if( |
| 156 | is_admin() |
| 157 | && Template_Cpt::TYPE == $current_page |
| 158 | && 'edit.php' == $pagenow |
| 159 | && isset($_GET['type']) |
| 160 | && $_GET['type'] != '' |
| 161 | && $_GET['type'] != 'all' |
| 162 | ) { |
| 163 | $type = isset($_GET['type']) ? sanitize_key($_GET['type']) : ''; |
| 164 | $query->query_vars['meta_key'] = Action::PK__SHOPENGINE_TEMPLATE . '__type'; |
| 165 | $query->query_vars['meta_value'] = $type; |
| 166 | $query->query_vars['meta_compare'] = '='; |
| 167 | } |
| 168 | } |
| 169 | |
| 170 | |
| 171 | /** |
| 172 | * Public function template_selected. |
| 173 | * add meta box for choose template ShopEngine |
| 174 | * |
| 175 | * @since 1.0.0 |
| 176 | */ |
| 177 | public function template_selected() { |
| 178 | global $post; |
| 179 | |
| 180 | if(in_array($post->post_type, $this->actionPost_type)): |
| 181 | foreach($this->actionPost_type as $k => $v): |
| 182 | add_meta_box( |
| 183 | 'shopengine_template', |
| 184 | esc_html__('ShopEngine Template', 'shopengine'), |
| 185 | [$this, 'shopengine_template'], |
| 186 | $v, |
| 187 | 'side', |
| 188 | 'low' |
| 189 | ); |
| 190 | endforeach; |
| 191 | endif; |
| 192 | } |
| 193 | |
| 194 | |
| 195 | /** |
| 196 | * Public function template_save. |
| 197 | * ShopEngine Template save for product |
| 198 | * |
| 199 | * @since 1.0.0 |
| 200 | */ |
| 201 | public function template_save($post_id, $post) { |
| 202 | if(!current_user_can('edit_post', $post_id)) { |
| 203 | return $post_id; |
| 204 | } |
| 205 | |
| 206 | if(in_array($post->post_type, $this->actionPost_type)): |
| 207 | if(isset($_POST['shopengine-template'])) { |
| 208 | update_post_meta($post_id, Action::PK__SHOPENGINE_TEMPLATE . '__template', sanitize_key($_POST['shopengine-template'])); |
| 209 | } |
| 210 | endif; |
| 211 | } |
| 212 | |
| 213 | |
| 214 | /** |
| 215 | * Public function shopengine_template. |
| 216 | * ShopEngine Template Html |
| 217 | * |
| 218 | * @since 1.0.0 |
| 219 | */ |
| 220 | public function shopengine_template() { |
| 221 | global $post; |
| 222 | |
| 223 | if(!isset($post->ID)) { |
| 224 | return ''; |
| 225 | } |
| 226 | |
| 227 | $page_template = get_post_meta($post->ID, Action::PK__SHOPENGINE_TEMPLATE . '__template', true); |
| 228 | |
| 229 | $template = $this->get_post_single(); |
| 230 | echo '<select name="shopengine-template">'; |
| 231 | echo '<option value="0"> ' . esc_html__('Defalut', 'shopengine') . ' </option>'; |
| 232 | if(is_array($template) && sizeof($template) > 0) { |
| 233 | foreach($template as $k => $v) { |
| 234 | $select = ''; |
| 235 | if($page_template == $k) { |
| 236 | $select = 'selected'; |
| 237 | } |
| 238 | echo '<option value="' . $k . '" ' . $select . '> ' . esc_html__($v, 'shopengine') . ' </option>'; |
| 239 | } |
| 240 | } |
| 241 | echo '</select>'; |
| 242 | } |
| 243 | |
| 244 | |
| 245 | /** |
| 246 | * get query post query |
| 247 | * |
| 248 | * @return array |
| 249 | */ |
| 250 | public function get_post_single() { |
| 251 | |
| 252 | $args['post_status'] = 'publish'; |
| 253 | $args['post_type'] = Template_Cpt::TYPE; |
| 254 | $args['meta_query'] = [ |
| 255 | 'relation' => 'AND', |
| 256 | array( |
| 257 | 'key' => Action::PK__SHOPENGINE_TEMPLATE . '__type', |
| 258 | 'value' => 'single', |
| 259 | 'compare' => '=', |
| 260 | ), |
| 261 | ]; |
| 262 | |
| 263 | $posts = get_posts($args); |
| 264 | $options = []; |
| 265 | $count = count($posts); |
| 266 | if($count > 0): |
| 267 | foreach($posts as $post) { |
| 268 | $options[$post->ID] = $post->post_title; |
| 269 | } |
| 270 | endif; |
| 271 | |
| 272 | return $options; |
| 273 | } |
| 274 | } |
| 275 |