Skip to content

Commit 0562208

Browse files
authored
Merge pull request #7 from PolyPlugins/version/1.0.7
1.0.7
2 parents d8bfc9e + 3d20c38 commit 0562208

3 files changed

Lines changed: 133 additions & 9 deletions

File tree

README.MD

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,14 @@ Yes, the plugin supports uploading images to the WordPress media library via the
6565

6666
## Changelog
6767

68+
### 1.0.7
69+
* Added: weight attribute to results of [Get Product](https://www.polyplugins.com/docs/content-api/api/get-product/) endpoint
70+
* Added: weight attribute to [Update Product](https://www.polyplugins.com/docs/content-api/api/update-product/) endpoint
71+
* Added: weight attribute to [Create Product](https://www.polyplugins.com/docs/content-api/api/create-product/) endpoint
72+
* Added: Option to show a notice on when the Content API was last accessed
73+
* Added: Option to show an error if the Content API has not been accessed in the last X amount of minutes
74+
* Bugfix: If stock passed as 0 it doesn't update
75+
6876
### 1.0.6
6977
* Added: upc attribute to results of [Get Product](https://www.polyplugins.com/docs/content-api/api/get-product/) endpoint
7078
* Added: upc attribute to [Update Product](https://www.polyplugins.com/docs/content-api/api/update-product/) endpoint

content-api.php

Lines changed: 116 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
* Plugin Name: Content API
55
* Plugin URI: https://www.polyplugins.com/contact/
66
* Description: Adds various endpoints to create content
7-
* Version: 1.0.6
7+
* Version: 1.0.7
88
* Requires at least: 6.5
99
* Requires PHP: 7.4
1010
* Author: Poly Plugins
@@ -37,6 +37,7 @@ public function init() {
3737
add_action('rest_api_init', array($this, 'register_endpoints'));
3838
add_action('admin_menu', array($this, 'register_settings_page'));
3939
add_action('admin_init', array($this, 'settings_page_init'));
40+
add_action('admin_notices', array($this, 'maybe_display_last_accessed_notice'));
4041
}
4142

4243
/**
@@ -424,6 +425,7 @@ public function get_product(WP_REST_Request $request) {
424425
$cost = $product->get_meta('_cost');
425426
$sku = $product->get_sku();
426427
$upc = $product->get_meta('_global_unique_id');
428+
$weight = $product->get_weight();
427429
$stock_status = $product->get_stock_status();
428430
$stock_quantity = $product->get_stock_quantity();
429431
$tags = wp_get_post_terms($product->get_id(), 'product_tag', array('fields' => 'names'));
@@ -459,6 +461,7 @@ public function get_product(WP_REST_Request $request) {
459461
'cost' => $cost ? floatval($cost) : '',
460462
'sku' => $sku ? sanitize_text_field($sku) : '',
461463
'upc' => $upc ? sanitize_text_field($upc) : '',
464+
'weight' => $weight ? floatval($weight) : '',
462465
'stock_status' => $stock_status ? sanitize_text_field($product->get_stock_status()) : '',
463466
'stock_quantity' => $stock_quantity ? absint($product->get_stock_quantity()) : 0,
464467
'tags' => $tags ? array_map('sanitize_text_field', $tags) : array(),
@@ -492,6 +495,7 @@ public function update_product(WP_REST_Request $request) {
492495
$cost = isset($fields['cost']) ? floatval($fields['cost']) : '';
493496
$sku = isset($fields['sku']) ? sanitize_text_field($fields['sku']) : '';
494497
$upc = isset($fields['upc']) ? sanitize_text_field($fields['upc']) : '';
498+
$weight = isset($fields['weight']) ? floatval($fields['weight']) : '';
495499
$stock_status = isset($fields['stock_status']) ? sanitize_text_field($fields['stock_status']) : '';
496500
$stock_quantity = isset($fields['stock_quantity']) ? intval($fields['stock_quantity']) : '';
497501
$tags = isset($fields['tags']) && is_array($fields['tags']) ? array_map('sanitize_text_field', $fields['tags']) : array();
@@ -599,11 +603,15 @@ public function update_product(WP_REST_Request $request) {
599603
$product->update_meta_data('_global_unique_id', $upc);
600604
}
601605

606+
if ($weight) {
607+
$product->set_weight($weight);
608+
}
609+
602610
if ($stock_status) {
603611
$product->set_stock_status($stock_status);
604612
}
605613

606-
if ($stock_quantity) {
614+
if ($stock_quantity >= 0) {
607615
$product->set_manage_stock(true);
608616
$product->set_stock_quantity($stock_quantity);
609617
}
@@ -714,6 +722,7 @@ public function create_product(WP_REST_Request $request) {
714722
$cost = isset($fields['cost']) ? floatval($fields['cost']) : '';
715723
$sku = isset($fields['sku']) ? sanitize_text_field($fields['sku']) : '';
716724
$upc = isset($fields['upc']) ? sanitize_text_field($fields['upc']) : '';
725+
$weight = isset($fields['weight']) ? floatval($fields['weight']) : '';
717726
$stock_status = isset($fields['stock_status']) ? sanitize_text_field($fields['stock_status']) : '';
718727
$stock_quantity = isset($fields['stock_quantity']) ? intval($fields['stock_quantity']) : '';
719728
$tags = isset($fields['tags']) && is_array($fields['tags']) ? array_map('sanitize_text_field', $fields['tags']) : array();
@@ -793,11 +802,15 @@ public function create_product(WP_REST_Request $request) {
793802
$product->update_meta_data('_global_unique_id', $upc);
794803
}
795804

805+
if ($weight) {
806+
$product->set_weight($weight);
807+
}
808+
796809
if ($stock_status) {
797810
$product->set_stock_status($stock_status);
798811
}
799812

800-
if ($stock_quantity) {
813+
if ($stock_quantity >= 0) {
801814
$product->set_manage_stock(true);
802815
$product->set_stock_quantity($stock_quantity);
803816
}
@@ -2236,9 +2249,25 @@ public function settings_page_init() {
22362249
'token', // id
22372250
'Token', // title
22382251
array($this, 'token_callback'), // callback
2239-
'content-api-settings', // page
2252+
'content-api-settings', // page
22402253
'setting_section' // section
22412254
);
2255+
2256+
add_settings_field(
2257+
'last_accessed_enabled',
2258+
'Last Accessed Notice',
2259+
array($this, 'last_accessed_enabled_callback'),
2260+
'content-api-settings',
2261+
'setting_section'
2262+
);
2263+
2264+
add_settings_field(
2265+
'last_accessed_error_time',
2266+
'Last Accessed Error Time',
2267+
array($this, 'last_accessed_error_time_callback'),
2268+
'content-api-settings',
2269+
'setting_section'
2270+
);
22422271
}
22432272

22442273
/**
@@ -2247,10 +2276,36 @@ public function settings_page_init() {
22472276
* @return void
22482277
*/
22492278
public function token_callback() {
2250-
printf(
2251-
'<input class="regular-text" type="password" name="content_api_options_polyplugins[token]" id="token" value="%s">',
2252-
isset($this->options['token']) ? esc_attr($this->options['token']) : ''
2253-
);
2279+
$option = isset($this->options['token']) ? sanitize_text_field($this->options['token']) : '';
2280+
?>
2281+
<input class="regular-text" type="password" name="content_api_options_polyplugins[token]" id="token" value="<?php echo esc_html($option); ?>">
2282+
<?php
2283+
}
2284+
2285+
/**
2286+
* Last Accessed Enabled callback
2287+
*
2288+
* @return void
2289+
*/
2290+
public function last_accessed_enabled_callback() {
2291+
$option = isset($this->options['last_accessed_enabled']) ? sanitize_text_field($this->options['last_accessed_enabled']) : '';
2292+
?>
2293+
<input type="checkbox" name="content_api_options_polyplugins[last_accessed_enabled]" id="last_accessed_enabled" <?php esc_attr(checked(1, $option, true)); ?> /> <?php echo esc_html__('Yes', 'content-api'); ?>
2294+
<p>This logs when the API was last accessed and displays a notice in the admin when it was last accessed.</p>
2295+
<?php
2296+
}
2297+
2298+
/**
2299+
* Last Accessed Error Time callback
2300+
*
2301+
* @return void
2302+
*/
2303+
public function last_accessed_error_time_callback() {
2304+
$option = isset($this->options['last_accessed_error_time']) ? sanitize_text_field($this->options['last_accessed_error_time']) : '';
2305+
?>
2306+
<input class="regular-text" type="number" name="content_api_options_polyplugins[last_accessed_error_time]" id="last_accessed_error_time" value="<?php echo esc_html($option); ?>">
2307+
<p>Enter the number of minutes that the notice for last accessed should be turned red.</p>
2308+
<?php
22542309
}
22552310

22562311
/**
@@ -2266,8 +2321,57 @@ public function sanitize($input) {
22662321
$sanitary_values['token'] = sanitize_text_field($input['token']);
22672322
}
22682323

2324+
if (isset($input['last_accessed_enabled']) && $input['last_accessed_enabled']) {
2325+
$sanitary_values['last_accessed_enabled'] = $input['last_accessed_enabled'] === 'on' ? true : false;
2326+
} else {
2327+
$sanitary_values['last_accessed_enabled'] = false;
2328+
}
2329+
2330+
if (isset($input['last_accessed_error_time']) && $input['last_accessed_error_time']) {
2331+
$sanitary_values['last_accessed_error_time'] = intval($input['last_accessed_error_time']);
2332+
}
2333+
22692334
return $sanitary_values;
22702335
}
2336+
2337+
/**
2338+
* Maybe display last access notice
2339+
*
2340+
* @param mixed $request
2341+
* @return true|WP_Error True if granted, error if not
2342+
*/
2343+
public function maybe_display_last_accessed_notice() {
2344+
$this->options = get_option('content_api_options_polyplugins');
2345+
2346+
if (!$this->options['last_accessed_enabled']) {
2347+
return;
2348+
}
2349+
2350+
$get_last_accessed = get_option('content_api_last_accessed_polyplugins');
2351+
2352+
if (!$get_last_accessed) {
2353+
return;
2354+
}
2355+
2356+
$last_accessed = intval($get_last_accessed);
2357+
$now = time();
2358+
$threshold = isset($this->options['last_accessed_error_time']) ? intval($this->options['last_accessed_error_time']) : 0;
2359+
$seconds_since_last_access = ($now - $last_accessed) / 60;
2360+
$is_error = $threshold && $seconds_since_last_access > $threshold;
2361+
$notice_class = $is_error ? 'notice-error' : 'notice-success';
2362+
$formatted_time = wp_date(get_option('date_format') . ' ' . get_option('time_format'), $last_accessed);
2363+
?>
2364+
<?php if (!$is_error) : ?>
2365+
<div class="notice <?php echo esc_attr($notice_class); ?>">
2366+
<p>Content API Last Accessed: <strong><?php echo esc_html($formatted_time); ?></strong></p>
2367+
</div>
2368+
<?php else : ?>
2369+
<div class="notice <?php echo esc_attr($notice_class); ?>" style="background-color: #D63638; color: #fff;">
2370+
<p>Content API Last Accessed: <strong><?php echo esc_html($formatted_time); ?></strong></p>
2371+
</div>
2372+
<?php endif; ?>
2373+
<?php
2374+
}
22712375

22722376
/**
22732377
* Checks if request is granted
@@ -2283,6 +2387,10 @@ public function has_permission(WP_REST_Request $request) {
22832387
if ($request_token !== $token) {
22842388
return new WP_Error('not_authorized', "Not Authorized", array('status' => 401));
22852389
}
2390+
2391+
if ($this->options['last_accessed_enabled']) {
2392+
update_option('content_api_last_accessed_polyplugins', time());
2393+
}
22862394

22872395
return true;
22882396
}

readme.txt

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
Contributors: polyplugins
33
Tags: content api, rest api, content, creative, api
44
Tested up to: 6.8
5-
Stable tag: 1.0.6
5+
Stable tag: 1.0.7
66
Requires PHP: 7.4
77
License: GPLv3
88
License URI: https://www.gnu.org/licenses/gpl-3.0.html
@@ -84,6 +84,14 @@ Yes, the plugin supports uploading images to the WordPress media library via the
8484

8585
== Changelog ==
8686

87+
= 1.0.7 =
88+
* Added: weight attribute to results of [Get Product](https://www.polyplugins.com/docs/content-api/api/get-product/) endpoint
89+
* Added: weight attribute to [Update Product](https://www.polyplugins.com/docs/content-api/api/update-product/) endpoint
90+
* Added: weight attribute to [Create Product](https://www.polyplugins.com/docs/content-api/api/create-product/) endpoint
91+
* Added: Option to show a notice on when the Content API was last accessed
92+
* Added: Option to show an error if the Content API has not been accessed in the last X amount of minutes
93+
* Bugfix: If stock passed as 0 it doesn't update
94+
8795
= 1.0.6 =
8896
* Added: upc attribute to results of [Get Product](https://www.polyplugins.com/docs/content-api/api/get-product/) endpoint
8997
* Added: upc attribute to [Update Product](https://www.polyplugins.com/docs/content-api/api/update-product/) endpoint

0 commit comments

Comments
 (0)