action.php
1 year ago
api.php
4 years ago
base.php
1 year ago
hooks.php
3 years ago
templates.php
3 years ago
hooks.php
245 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 | public $languages = []; |
| 19 | public $activated_templates = []; |
| 20 | |
| 21 | public function init() { |
| 22 | |
| 23 | $this->action = new Action(); |
| 24 | $cptName = Template_Cpt::TYPE; |
| 25 | |
| 26 | // check admin init |
| 27 | add_action('admin_init', [$this, 'add_author_support'], 10); |
| 28 | add_filter('manage_' . $cptName . '_posts_columns', [$this, 'set_columns']); |
| 29 | add_action('manage_' . $cptName . '_posts_custom_column', [$this, 'render_column'], 10, 2); |
| 30 | |
| 31 | // add filter for search |
| 32 | add_action('restrict_manage_posts', [$this, 'add_filter']); |
| 33 | // query filter |
| 34 | add_filter('parse_query', [$this, 'query_filter']); |
| 35 | |
| 36 | add_action('elementor/editor/init', [$this, 'elementor_editor_initialized']); |
| 37 | |
| 38 | // override the default notice template. |
| 39 | add_filter('woocommerce_locate_template', [$this, 'shopengine_notice_template'], 10, 3); |
| 40 | } |
| 41 | |
| 42 | /** |
| 43 | * On shopengine template elementor editor |
| 44 | * Check if the shopengine product id exists for single template |
| 45 | * |
| 46 | * @since 2.5.0 |
| 47 | */ |
| 48 | public function elementor_editor_initialized() |
| 49 | { |
| 50 | global $post; |
| 51 | // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- This hook can access only admin and already verified from elementor |
| 52 | if ( !empty( $_GET['action'] ) && sanitize_text_field( wp_unslash( $_GET['action'] ) ) === 'elementor' && get_post_type( $post ) === 'shopengine-template' ) { |
| 53 | $template_type = get_post_meta( $post->ID, 'shopengine_template__post_meta__type', true ); |
| 54 | $checkable_template_type = ['single', 'quick_view']; |
| 55 | // Check if the template is single or quick view template |
| 56 | if ( in_array( $template_type, $checkable_template_type ) ) { |
| 57 | // phpcs:ignore WordPress.Security.NonceVerification.Recommended |
| 58 | if ( empty( $_GET['shopengine_product_id'] ) || !wc_get_product( intval( $_GET['shopengine_product_id'] ) ) ) { |
| 59 | wp_safe_redirect( Helper::get_admin_list_template_url() ); |
| 60 | exit(); |
| 61 | } |
| 62 | } |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | /** |
| 67 | * Public function add_author_support. |
| 68 | * check author support |
| 69 | * |
| 70 | * @since 1.0.0 |
| 71 | */ |
| 72 | public function add_author_support() { |
| 73 | $this->languages = apply_filters('shopengine_multi_language', ['status' => false, 'lang_items' => []]); |
| 74 | $this->activated_templates = Action::get_activated_templates(); |
| 75 | |
| 76 | add_post_type_support(Template_Cpt::TYPE, 'author'); |
| 77 | } |
| 78 | |
| 79 | |
| 80 | /** |
| 81 | * Public function set_columns. |
| 82 | * set column for custom post type |
| 83 | * |
| 84 | * @since 1.0.0 |
| 85 | */ |
| 86 | public function set_columns($columns) { |
| 87 | |
| 88 | $date_column = $columns['date']; |
| 89 | $author_column = $columns['author']; |
| 90 | |
| 91 | unset($columns['date']); |
| 92 | unset($columns['author']); |
| 93 | |
| 94 | $columns['type'] = esc_html__('Type', 'shopengine'); |
| 95 | |
| 96 | if(true === $this->languages['status']) { |
| 97 | $columns['lang'] = esc_html__('Language', 'shopengine'); |
| 98 | } |
| 99 | |
| 100 | $columns['status'] = esc_html__('Status', 'shopengine'); |
| 101 | $columns['builder'] = esc_html__('Builder', 'shopengine'); |
| 102 | $columns['author'] = esc_html($author_column); |
| 103 | $columns['date'] = esc_html($date_column); |
| 104 | |
| 105 | return $columns; |
| 106 | } |
| 107 | |
| 108 | /** |
| 109 | * Public function render_column. |
| 110 | * Render column for custom post type |
| 111 | * |
| 112 | * @param $column |
| 113 | * @param $post_id |
| 114 | * @since 1.0.0 |
| 115 | * |
| 116 | */ |
| 117 | public function render_column($column, $post_id) { |
| 118 | |
| 119 | $data = get_post_meta($post_id, Action::PK__SHOPENGINE_TEMPLATE, true); |
| 120 | $template_type = isset($data['form_type']) ? $data['form_type'] : ''; |
| 121 | $template_config_data = Page_Templates::instance()->getTemplate($template_type); |
| 122 | $template_class = $template_config_data['class'] ?? null; |
| 123 | $category_id = !empty($data['category_id']) ? $data['category_id'] : 0; |
| 124 | $template_language = get_post_meta($post_id, 'language_code', true); |
| 125 | |
| 126 | switch($column) { |
| 127 | case 'type': |
| 128 | echo esc_html(empty($template_config_data['title']) ? '' : $template_config_data['title']); |
| 129 | if(class_exists(\ShopEngine_Pro::class)) { |
| 130 | $cat_name = get_the_category_by_ID($category_id); |
| 131 | if(isset($cat_name) && !is_wp_error($cat_name)) { |
| 132 | echo '<br>' . esc_html__('Category', 'shopengine') .' : '. esc_html($cat_name); |
| 133 | } |
| 134 | } |
| 135 | break; |
| 136 | |
| 137 | case 'lang': |
| 138 | if(!empty($this->languages['lang_items'][$template_language]['country_flag_url'])) { |
| 139 | ?> |
| 140 | <img src="<?php echo esc_url($this->languages['lang_items'][$template_language]['country_flag_url'])?>"> |
| 141 | <?php |
| 142 | } |
| 143 | break; |
| 144 | |
| 145 | case 'builder': |
| 146 | $builder = Helper::get_template_builder_type($post_id);; |
| 147 | echo esc_html(empty($builder) ? 'elementor' : $builder); |
| 148 | break; |
| 149 | |
| 150 | case 'status': |
| 151 | |
| 152 | $status = esc_html__('Inactive', 'shopengine'); |
| 153 | $status_class = 'shopengine-deactive'; |
| 154 | |
| 155 | if(class_exists($template_class)) { |
| 156 | $template_data = Action::get_template_data($post_id, $this->activated_templates); |
| 157 | if('en' === $template_language) { |
| 158 | if(is_array($template_data) && $template_data['status']) { |
| 159 | $status = esc_html__('Active', 'shopengine'); |
| 160 | $status_class = 'shopengine-active'; |
| 161 | } |
| 162 | } elseif($this->languages['status'] && is_array($template_data) && $template_data['status']) { |
| 163 | $status = esc_html__('Active', 'shopengine'); |
| 164 | $status_class = 'shopengine-active'; |
| 165 | } |
| 166 | } |
| 167 | |
| 168 | echo wp_kses('<span class="shopengine_default type-'.$template_type . ' ' . $status_class.'"> ' . $status . ' </span>', Helper::get_kses_array()); |
| 169 | break; |
| 170 | } |
| 171 | } |
| 172 | |
| 173 | /** |
| 174 | * Public function add_filter. |
| 175 | * Added search filter for type of template |
| 176 | * |
| 177 | * @since 1.0.0 |
| 178 | */ |
| 179 | public function add_filter() { |
| 180 | |
| 181 | global $typenow; |
| 182 | |
| 183 | if($typenow == Template_Cpt::TYPE) { |
| 184 | |
| 185 | // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- This hook can access only admin And not possible to verify nonce here |
| 186 | $selected = isset($_GET['type']) ? sanitize_key($_GET['type']) : ''; ?> |
| 187 | |
| 188 | <select name="type" id="type"> |
| 189 | |
| 190 | <option value="all" <?php selected('all', $selected); ?>><?php esc_html_e('Template Type ', 'shopengine'); ?></option> <?php |
| 191 | |
| 192 | foreach(Templates::get_template_types() as $key => $value) { ?> |
| 193 | <option value="<?php echo esc_attr($key); ?>" <?php selected($key, $selected); ?>><?php esc_html_e($value['title'], 'shopengine'); ?></option> |
| 194 | <?php } ?> |
| 195 | |
| 196 | </select> |
| 197 | <?php |
| 198 | } |
| 199 | } |
| 200 | |
| 201 | /** |
| 202 | * Public function query_filter. |
| 203 | * Search query filter added in search query |
| 204 | * |
| 205 | * @param $query |
| 206 | * @since 1.0.0 |
| 207 | */ |
| 208 | public function query_filter($query) { |
| 209 | |
| 210 | global $pagenow; |
| 211 | // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- This hook can access only admin And not possible to verify nonce here |
| 212 | $current_page = isset($_GET['post_type']) ? sanitize_key($_GET['post_type']) : ''; |
| 213 | |
| 214 | if( |
| 215 | is_admin() |
| 216 | && Template_Cpt::TYPE == $current_page |
| 217 | && 'edit.php' == $pagenow |
| 218 | && !empty($_GET['type']) // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- This hook can access only admin And not possible to verify nonce here |
| 219 | && $_GET['type'] != 'all' // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- This hook can access only admin And not possible to verify nonce here |
| 220 | ) { |
| 221 | // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- This hook can access only admin And not possible to verify nonce here |
| 222 | $type = sanitize_key($_GET['type']); |
| 223 | $query->query_vars['meta_key'] = Action::get_meta_key_for_type(); |
| 224 | $query->query_vars['meta_value'] = $type; |
| 225 | $query->query_vars['meta_compare'] = '='; |
| 226 | } |
| 227 | } |
| 228 | |
| 229 | /** |
| 230 | * Change the default notice template. |
| 231 | * |
| 232 | * @param $template |
| 233 | * @param $template_name |
| 234 | * @param $template_path |
| 235 | * @since 4.2.0 |
| 236 | */ |
| 237 | public function shopengine_notice_template($template, $template_name, $template_path) { |
| 238 | if ($template_name === 'notices/notice.php') { |
| 239 | $template = \Shopengine::plugin_dir() . 'woocommerce/notices/notice.php'; |
| 240 | } |
| 241 | return $template; |
| 242 | } |
| 243 | |
| 244 | } |
| 245 |