screens
4 years ago
action.php
2 years ago
api.php
1 day ago
base.php
4 months ago
plugin-status.php
3 years ago
api.php
279 lines
| 1 | <?php |
| 2 | namespace ShopEngine\Core\Settings; |
| 3 | |
| 4 | use ShopEngine\Core\Onboard\Onboard; |
| 5 | use ShopEngine\Core\Register\Model; |
| 6 | |
| 7 | defined('ABSPATH') || exit; |
| 8 | |
| 9 | /** |
| 10 | * Class Api |
| 11 | * |
| 12 | * @package ShopEngine\Core\Builders |
| 13 | */ |
| 14 | class Api extends \ShopEngine\Base\Api { |
| 15 | |
| 16 | public function config() { |
| 17 | |
| 18 | $this->prefix = 'settings'; |
| 19 | $this->param = ""; |
| 20 | $this->only_admin = true; |
| 21 | } |
| 22 | |
| 23 | |
| 24 | public function post_save() { |
| 25 | |
| 26 | if( !wp_verify_nonce( $this->request->get_header('x_wp_nonce'), 'wp_rest') || !current_user_can( 'manage_options' ) ) { |
| 27 | return false; |
| 28 | } |
| 29 | |
| 30 | $data = json_decode($this->request->get_body(), true); |
| 31 | |
| 32 | if(!empty($data['widgets'])) { |
| 33 | |
| 34 | Model::source('settings')->set_option('widgets', $data['widgets']); |
| 35 | } |
| 36 | |
| 37 | if(!empty($data['modules'])) { |
| 38 | |
| 39 | Model::source('settings')->set_option('modules', $data['modules']); |
| 40 | } |
| 41 | |
| 42 | if(!empty($data['userdata'])) { |
| 43 | |
| 44 | Model::source('settings')->set_option('userdata', $data['userdata']); |
| 45 | } |
| 46 | |
| 47 | do_action('shopengine/core/settings/on_save', $data); |
| 48 | |
| 49 | return [ |
| 50 | 'status' => 'success', |
| 51 | 'message' => esc_html__('settings saved successfully.', 'shopengine'), |
| 52 | ]; |
| 53 | } |
| 54 | |
| 55 | |
| 56 | public function get_fields() { |
| 57 | $fields = array_merge( |
| 58 | Action::instance()->get_fields(), |
| 59 | ['sample_designs' => \ShopEngine\Core\Sample_Designs\Base::instance()->get_designs()] |
| 60 | ); |
| 61 | |
| 62 | return apply_filters('shopengine/core/settings/return_fields', $fields); |
| 63 | } |
| 64 | |
| 65 | public function get_data() { |
| 66 | $data = Action::instance()->get_data(); |
| 67 | |
| 68 | return apply_filters('shopengine/core/settings/return_data', $data); |
| 69 | } |
| 70 | |
| 71 | public function get_our_others_plugin_install_api() { |
| 72 | $plugins = [ |
| 73 | 'elementskit-lite' => 'elementskit-lite.php', |
| 74 | 'rox-dynamic-cpt-fields-engine' => 'rox-dynamic-cpt-fields-engine.php', |
| 75 | 'rox-appointment-booking' => 'rox-appointment-booking.php', |
| 76 | 'metform' => 'metform.php', |
| 77 | 'gutenkit-blocks-addon' => 'gutenkit-blocks-addon.php', |
| 78 | 'popup-builder-block' => 'popup-builder-block.php', |
| 79 | 'emailkit' => 'EmailKit.php', |
| 80 | 'getgenie' => 'getgenie.php', |
| 81 | ]; |
| 82 | |
| 83 | $plugin_status = Plugin_Status::instance(); |
| 84 | $plugins_data = []; |
| 85 | |
| 86 | foreach($plugins as $slug => $file) { |
| 87 | $plugins_data[$slug] = $plugin_status->get_status($slug.'/'.$file); |
| 88 | } |
| 89 | return $plugins_data; |
| 90 | } |
| 91 | |
| 92 | public function post_save_onboard() { |
| 93 | $data = $this->request->get_params(); |
| 94 | $onboard = new Onboard(); |
| 95 | return $onboard->submit($data); |
| 96 | } |
| 97 | |
| 98 | public function get_categories() { |
| 99 | |
| 100 | $data = $this->request->get_params(); |
| 101 | |
| 102 | $query_args = [ |
| 103 | 'taxonomy' => ['product_cat'], // taxonomy name |
| 104 | 'orderby' => 'name', |
| 105 | 'order' => 'DESC', |
| 106 | 'hide_empty' => false, |
| 107 | 'number' => isset($data['number']) ? absint($data['number']) : 0 // limit number of terms, 0 = no limit |
| 108 | ]; |
| 109 | |
| 110 | if(isset($data['only_parent'])){ |
| 111 | $query_args['parent'] = 0; |
| 112 | } |
| 113 | |
| 114 | // Allow requesting children for a specific parent id |
| 115 | if (isset($data['parent'])) { |
| 116 | $query_args['parent'] = absint($data['parent']); |
| 117 | } |
| 118 | |
| 119 | if(isset($data['ids'])){ |
| 120 | $ids = explode(',', $data['ids']); |
| 121 | $query_args['include'] = $ids; |
| 122 | } |
| 123 | if(isset($data['s'])){ |
| 124 | $query_args['name__like'] = $data['s']; |
| 125 | } |
| 126 | |
| 127 | $product_cat = get_terms($query_args); |
| 128 | $product_categories = []; |
| 129 | $categories_with_children_info = []; |
| 130 | |
| 131 | foreach($product_cat as $category) { |
| 132 | $product_categories[$category->term_id] = $category->name; |
| 133 | |
| 134 | // Check if this category has children |
| 135 | $children_count = get_terms([ |
| 136 | 'taxonomy' => 'product_cat', |
| 137 | 'parent' => $category->term_id, |
| 138 | 'hide_empty' => false, |
| 139 | 'fields' => 'count' |
| 140 | ]); |
| 141 | |
| 142 | $categories_with_children_info[$category->term_id] = [ |
| 143 | 'name' => $category->name, |
| 144 | 'has_children' => $children_count > 0 |
| 145 | ]; |
| 146 | } |
| 147 | |
| 148 | // If a number limit is provided, also report if there are more children available |
| 149 | $more = 0; |
| 150 | if (!empty($data['number'])) { |
| 151 | // If parent is set, count total children for that parent; otherwise count top-level if only_parent |
| 152 | $count_args = $query_args; |
| 153 | // remove number limit for counting |
| 154 | unset($count_args['number']); |
| 155 | $all_terms = get_terms($count_args); |
| 156 | $total = is_array($all_terms) ? count($all_terms) : 0; |
| 157 | $more = max(0, $total - intval($data['number'])); |
| 158 | } |
| 159 | |
| 160 | return [ |
| 161 | 'status' => 'success', |
| 162 | 'result' => $product_categories, |
| 163 | 'result_with_children' => $categories_with_children_info, |
| 164 | 'more' => $more, |
| 165 | 'message' => esc_html__('categories fetched', 'shopengine') |
| 166 | ]; |
| 167 | } |
| 168 | |
| 169 | public function get_posts() { |
| 170 | |
| 171 | $data = $this->request->get_params(); |
| 172 | |
| 173 | if(empty($data['post_type'])) { |
| 174 | return [ |
| 175 | 'status' => 'failed' |
| 176 | ]; |
| 177 | } |
| 178 | |
| 179 | $search = isset($data['s']) ? $data['s'] : false; |
| 180 | $post_status = !empty($data['post_status']) ? $data['post_status'] : ''; |
| 181 | |
| 182 | global $wpdb; |
| 183 | |
| 184 | $params = [ |
| 185 | sanitize_text_field($data['post_type']) |
| 186 | ]; |
| 187 | |
| 188 | $post_status_array = ['publish']; |
| 189 | if($post_status === 'draft'){ |
| 190 | $post_status_array[] = 'draft'; |
| 191 | } |
| 192 | |
| 193 | $escaped = array(); |
| 194 | foreach($post_status_array as $status_item){ |
| 195 | $escaped[] = $wpdb->prepare('%s', sanitize_text_field($status_item)); |
| 196 | } |
| 197 | $post_status = implode(',', $escaped); |
| 198 | |
| 199 | $post_search_statement = ''; |
| 200 | if(!empty($search)){ |
| 201 | $post_search_statement = 'AND post_title LIKE %s'; |
| 202 | array_push($params, '%'. $wpdb->esc_like( $search ) .'%'); |
| 203 | } |
| 204 | |
| 205 | //phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared -- Already applied prepare method in top |
| 206 | $posts = $wpdb->get_results( $wpdb->prepare("SELECT * FROM $wpdb->posts WHERE post_type=%s AND post_status IN ($post_status) $post_search_statement LIMIT 10", $params) ); |
| 207 | |
| 208 | $post_items = []; |
| 209 | foreach($posts as $post) { |
| 210 | array_push($post_items, ['id' => $post->ID, 'text' => $post->post_title]); |
| 211 | } |
| 212 | |
| 213 | return [ |
| 214 | 'status' => 'success', |
| 215 | 'results' => $post_items, |
| 216 | ]; |
| 217 | } |
| 218 | public function post_onboard_plugins() { |
| 219 | |
| 220 | // Get plugin_slug directly from request parameters |
| 221 | $plugin_slug = $this->request->get_param('plugin_slug'); |
| 222 | |
| 223 | if (empty($plugin_slug)) { |
| 224 | return [ |
| 225 | 'success' => false, |
| 226 | 'message' => 'Plugin slug is required' |
| 227 | ]; |
| 228 | } |
| 229 | |
| 230 | if (!current_user_can('install_plugins')) { |
| 231 | return [ |
| 232 | 'success' => false, |
| 233 | 'message' => 'Insufficient permissions to install plugins' |
| 234 | ]; |
| 235 | } |
| 236 | |
| 237 | |
| 238 | $status = \ShopEngine\Utils\Onboard\Plugin_Installer::single_install_and_activate($plugin_slug); |
| 239 | |
| 240 | if ($status === false) { |
| 241 | return [ |
| 242 | 'success' => false, |
| 243 | 'message' => 'Plugin installation failed' |
| 244 | ]; |
| 245 | } else { |
| 246 | return [ |
| 247 | 'success' => true, |
| 248 | 'data' => [ |
| 249 | 'message' => self::plugin_activate_message($plugin_slug) |
| 250 | ] |
| 251 | ]; |
| 252 | } |
| 253 | } |
| 254 | |
| 255 | public static function plugin_activate_message($plugin_slug) { |
| 256 | $plugins_message = [ |
| 257 | 'setup_configurations' => esc_html__('Setup Configurations', 'shopengine'), |
| 258 | 'elementskit-lite/elementskit-lite.php' => esc_html__('Page Builder Elements Activated', 'shopengine'), |
| 259 | 'rox-dynamic-cpt-fields-engine/rox-dynamic-cpt-fields-engine.php' => esc_html__('Dynamic CPT Fields Activated', 'shopengine'), |
| 260 | 'rox-appointment-booking/rox-appointment-booking.php' => esc_html__('Appointment Booking Activated', 'shopengine'), |
| 261 | 'getgenie/getgenie.php' => esc_html__('AI Content & SEO Tool Activated', 'shopengine'), |
| 262 | 'shopengine/shopengine.php' => esc_html__('WooCommerce Builder Activated', 'shopengine'), |
| 263 | 'metform/metform.php' => esc_html__('Form Builder Activated', 'shopengine'), |
| 264 | 'emailkit/EmailKit.php' => esc_html__('Email Customizer Activated', 'shopengine'), |
| 265 | 'gutenkit-blocks-addon/gutenkit-blocks-addon.php' => esc_html__('Page Builder Blocks Activated', 'shopengine'), |
| 266 | 'popup-builder-block/popup-builder-block.php' => esc_html__('Popup Builder Activated', 'shopengine'), |
| 267 | 'table-builder-block/table-builder-block.php' => esc_html__('Table Builder Activated', 'shopengine'), |
| 268 | ]; |
| 269 | |
| 270 | if ( array_key_exists( $plugin_slug, $plugins_message ) ) { |
| 271 | return esc_html( $plugins_message[$plugin_slug] ); |
| 272 | } else { |
| 273 | return esc_html__( 'Plugin Activated', 'shopengine' ); |
| 274 | } |
| 275 | } |
| 276 | } |
| 277 | |
| 278 | |
| 279 |