| 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 |
]; |
| 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' => 10 |
| 108 |
]; |
| 109 |
|
| 110 |
if(isset($data['only_parent'])){ |
| 111 |
$query_args['parent'] = 0; |
| 112 |
} |
| 113 |
|
| 114 |
if(isset($data['ids'])){ |
| 115 |
$ids = explode(',', $data['ids']); |
| 116 |
$query_args['include'] = $ids; |
| 117 |
} |
| 118 |
if(isset($data['s'])){ |
| 119 |
$query_args['name__like'] = $data['s']; |
| 120 |
} |
| 121 |
|
| 122 |
$product_cat = get_terms($query_args); |
| 123 |
$product_categories = []; |
| 124 |
foreach($product_cat as $category) { |
| 125 |
$product_categories[$category->term_id] = $category->name; |
| 126 |
} |
| 127 |
return [ |
| 128 |
'status' => 'success', |
| 129 |
'result' => $product_categories, |
| 130 |
'message' => esc_html__('categories fetched', 'shopengine') |
| 131 |
]; |
| 132 |
} |
| 133 |
|
| 134 |
public function get_posts() { |
| 135 |
|
| 136 |
$data = $this->request->get_params(); |
| 137 |
|
| 138 |
if(empty($data['post_type'])) { |
| 139 |
return [ |
| 140 |
'status' => 'failed' |
| 141 |
]; |
| 142 |
} |
| 143 |
|
| 144 |
$search = isset($data['s']) ? $data['s'] : false; |
| 145 |
$post_status = !empty($data['post_status']) ? $data['post_status'] : ''; |
| 146 |
|
| 147 |
global $wpdb; |
| 148 |
|
| 149 |
$params = [ |
| 150 |
sanitize_text_field($data['post_type']) |
| 151 |
]; |
| 152 |
|
| 153 |
$post_status_array = ['publish']; |
| 154 |
if($post_status === 'draft'){ |
| 155 |
$post_status_array[] = 'draft'; |
| 156 |
} |
| 157 |
|
| 158 |
$escaped = array(); |
| 159 |
foreach($post_status_array as $status_item){ |
| 160 |
$escaped[] = $wpdb->prepare('%s', sanitize_text_field($status_item)); |
| 161 |
} |
| 162 |
$post_status = implode(',', $escaped); |
| 163 |
|
| 164 |
$post_search_statement = ''; |
| 165 |
if(!empty($search)){ |
| 166 |
$post_search_statement = 'AND post_title LIKE %s'; |
| 167 |
array_push($params, '%'. $wpdb->esc_like( $search ) .'%'); |
| 168 |
} |
| 169 |
|
| 170 |
//phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared -- Already applied prepare method in top |
| 171 |
$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) ); |
| 172 |
|
| 173 |
$post_items = []; |
| 174 |
foreach($posts as $post) { |
| 175 |
array_push($post_items, ['id' => $post->ID, 'text' => $post->post_title]); |
| 176 |
} |
| 177 |
|
| 178 |
return [ |
| 179 |
'status' => 'success', |
| 180 |
'results' => $post_items, |
| 181 |
]; |
| 182 |
} |
| 183 |
} |
| 184 |
|
| 185 |
|
| 186 |
|