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