| 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 |
'metform' => 'metform.php', |
| 75 |
'wp-social' => 'wp-social.php', |
| 76 |
'wp-ultimate-review' => 'wp-ultimate-review.php', |
| 77 |
'wp-fundraising-donation' => 'wp-fundraising-donation.php', |
| 78 |
'getgenie' => 'getgenie.php', |
| 79 |
'emailkit' => 'EmailKit.php', |
| 80 |
'gutenkit-blocks-addon' => 'gutenkit-blocks-addon.php', |
| 81 |
'popup-builder-block' => 'popup-builder-block.php', |
| 82 |
]; |
| 83 |
|
| 84 |
$plugin_status = Plugin_Status::instance(); |
| 85 |
$plugins_data = []; |
| 86 |
|
| 87 |
foreach($plugins as $slug => $file) { |
| 88 |
$plugins_data[$slug] = $plugin_status->get_status($slug.'/'.$file); |
| 89 |
} |
| 90 |
return $plugins_data; |
| 91 |
} |
| 92 |
|
| 93 |
public function post_save_onboard() { |
| 94 |
$data = $this->request->get_params(); |
| 95 |
$onboard = new Onboard(); |
| 96 |
return $onboard->submit($data); |
| 97 |
} |
| 98 |
|
| 99 |
public function get_categories() { |
| 100 |
|
| 101 |
$data = $this->request->get_params(); |
| 102 |
|
| 103 |
$query_args = [ |
| 104 |
'taxonomy' => ['product_cat'], // taxonomy name |
| 105 |
'orderby' => 'name', |
| 106 |
'order' => 'DESC', |
| 107 |
'hide_empty' => false, |
| 108 |
'number' => isset($data['number']) ? absint($data['number']) : 0 // limit number of terms, 0 = no limit |
| 109 |
]; |
| 110 |
|
| 111 |
if(isset($data['only_parent'])){ |
| 112 |
$query_args['parent'] = 0; |
| 113 |
} |
| 114 |
|
| 115 |
// Allow requesting children for a specific parent id |
| 116 |
if (isset($data['parent'])) { |
| 117 |
$query_args['parent'] = absint($data['parent']); |
| 118 |
} |
| 119 |
|
| 120 |
if(isset($data['ids'])){ |
| 121 |
$ids = explode(',', $data['ids']); |
| 122 |
$query_args['include'] = $ids; |
| 123 |
} |
| 124 |
if(isset($data['s'])){ |
| 125 |
$query_args['name__like'] = $data['s']; |
| 126 |
} |
| 127 |
|
| 128 |
$product_cat = get_terms($query_args); |
| 129 |
$product_categories = []; |
| 130 |
$categories_with_children_info = []; |
| 131 |
|
| 132 |
foreach($product_cat as $category) { |
| 133 |
$product_categories[$category->term_id] = $category->name; |
| 134 |
|
| 135 |
// Check if this category has children |
| 136 |
$children_count = get_terms([ |
| 137 |
'taxonomy' => 'product_cat', |
| 138 |
'parent' => $category->term_id, |
| 139 |
'hide_empty' => false, |
| 140 |
'fields' => 'count' |
| 141 |
]); |
| 142 |
|
| 143 |
$categories_with_children_info[$category->term_id] = [ |
| 144 |
'name' => $category->name, |
| 145 |
'has_children' => $children_count > 0 |
| 146 |
]; |
| 147 |
} |
| 148 |
|
| 149 |
// If a number limit is provided, also report if there are more children available |
| 150 |
$more = 0; |
| 151 |
if (!empty($data['number'])) { |
| 152 |
// If parent is set, count total children for that parent; otherwise count top-level if only_parent |
| 153 |
$count_args = $query_args; |
| 154 |
// remove number limit for counting |
| 155 |
unset($count_args['number']); |
| 156 |
$all_terms = get_terms($count_args); |
| 157 |
$total = is_array($all_terms) ? count($all_terms) : 0; |
| 158 |
$more = max(0, $total - intval($data['number'])); |
| 159 |
} |
| 160 |
|
| 161 |
return [ |
| 162 |
'status' => 'success', |
| 163 |
'result' => $product_categories, |
| 164 |
'result_with_children' => $categories_with_children_info, |
| 165 |
'more' => $more, |
| 166 |
'message' => esc_html__('categories fetched', 'shopengine') |
| 167 |
]; |
| 168 |
} |
| 169 |
|
| 170 |
public function get_posts() { |
| 171 |
|
| 172 |
$data = $this->request->get_params(); |
| 173 |
|
| 174 |
if(empty($data['post_type'])) { |
| 175 |
return [ |
| 176 |
'status' => 'failed' |
| 177 |
]; |
| 178 |
} |
| 179 |
|
| 180 |
$search = isset($data['s']) ? $data['s'] : false; |
| 181 |
$post_status = !empty($data['post_status']) ? $data['post_status'] : ''; |
| 182 |
|
| 183 |
global $wpdb; |
| 184 |
|
| 185 |
$params = [ |
| 186 |
sanitize_text_field($data['post_type']) |
| 187 |
]; |
| 188 |
|
| 189 |
$post_status_array = ['publish']; |
| 190 |
if($post_status === 'draft'){ |
| 191 |
$post_status_array[] = 'draft'; |
| 192 |
} |
| 193 |
|
| 194 |
$escaped = array(); |
| 195 |
foreach($post_status_array as $status_item){ |
| 196 |
$escaped[] = $wpdb->prepare('%s', sanitize_text_field($status_item)); |
| 197 |
} |
| 198 |
$post_status = implode(',', $escaped); |
| 199 |
|
| 200 |
$post_search_statement = ''; |
| 201 |
if(!empty($search)){ |
| 202 |
$post_search_statement = 'AND post_title LIKE %s'; |
| 203 |
array_push($params, '%'. $wpdb->esc_like( $search ) .'%'); |
| 204 |
} |
| 205 |
|
| 206 |
//phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared -- Already applied prepare method in top |
| 207 |
$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) ); |
| 208 |
|
| 209 |
$post_items = []; |
| 210 |
foreach($posts as $post) { |
| 211 |
array_push($post_items, ['id' => $post->ID, 'text' => $post->post_title]); |
| 212 |
} |
| 213 |
|
| 214 |
return [ |
| 215 |
'status' => 'success', |
| 216 |
'results' => $post_items, |
| 217 |
]; |
| 218 |
} |
| 219 |
public function post_onboard_plugins() { |
| 220 |
|
| 221 |
// Get plugin_slug directly from request parameters |
| 222 |
$plugin_slug = $this->request->get_param('plugin_slug'); |
| 223 |
|
| 224 |
if (empty($plugin_slug)) { |
| 225 |
return [ |
| 226 |
'success' => false, |
| 227 |
'message' => 'Plugin slug is required' |
| 228 |
]; |
| 229 |
} |
| 230 |
|
| 231 |
if (!current_user_can('install_plugins')) { |
| 232 |
return [ |
| 233 |
'success' => false, |
| 234 |
'message' => 'Insufficient permissions to install plugins' |
| 235 |
]; |
| 236 |
} |
| 237 |
|
| 238 |
|
| 239 |
$status = \ShopEngine\Utils\Onboard\Plugin_Installer::single_install_and_activate($plugin_slug); |
| 240 |
|
| 241 |
if (is_wp_error($status)) { |
| 242 |
return [ |
| 243 |
'success' => false, |
| 244 |
wp_send_json_error( array( 'status' => false ) ), |
| 245 |
]; |
| 246 |
} else { |
| 247 |
return [ |
| 248 |
'success' => true, |
| 249 |
'data' => [ |
| 250 |
'message' => self::plugin_activate_message($plugin_slug) |
| 251 |
] |
| 252 |
]; |
| 253 |
} |
| 254 |
} |
| 255 |
|
| 256 |
public static function plugin_activate_message($plugin_slug) { |
| 257 |
$plugins_message = [ |
| 258 |
'setup_configurations' => esc_html__('Setup Configurations', 'shopengine'), |
| 259 |
'elementskit-lite/elementskit-lite.php' => esc_html__('Page Builder Elements Activated', 'shopengine'), |
| 260 |
'getgenie/getgenie.php' => esc_html__('AI Content & SEO Tool Activated', 'shopengine'), |
| 261 |
'shopengine/shopengine.php' => esc_html__('WooCommerce Builder Activated', 'shopengine'), |
| 262 |
'metform/metform.php' => esc_html__('Form Builder Activated', 'shopengine'), |
| 263 |
'emailkit/EmailKit.php' => esc_html__('Email Customizer Activated', 'shopengine'), |
| 264 |
'wp-social/wp-social.php' => esc_html__('Social Integration Activated', 'shopengine'), |
| 265 |
'wp-ultimate-review/wp-ultimate-review.php' => esc_html__('Review Management Activated', 'shopengine'), |
| 266 |
'wp-fundraising-donation/wp-fundraising.php' => esc_html__('Fundraising & Donations', 'shopengine'), |
| 267 |
'gutenkit-blocks-addon/gutenkit-blocks-addon.php' => esc_html__('Page Builder Blocks Activated', 'shopengine'), |
| 268 |
'popup-builder-block/popup-builder-block.php' => esc_html__('Popup Builder Activated', 'shopengine'), |
| 269 |
'table-builder-block/table-builder-block.php' => esc_html__('Table Builder Activated', 'shopengine'), |
| 270 |
]; |
| 271 |
|
| 272 |
if ( array_key_exists( $plugin_slug, $plugins_message ) ) { |
| 273 |
return esc_html( $plugins_message[$plugin_slug] ); |
| 274 |
} else { |
| 275 |
return esc_html__( 'Plugin Activated', 'shopengine' ); |
| 276 |
} |
| 277 |
} |
| 278 |
} |
| 279 |
|
| 280 |
|
| 281 |
|