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