diff --git a/js/gutenberg/blocks.build.js b/js/gutenberg/blocks.build.js index b9dde94..512cac3 100644 --- a/js/gutenberg/blocks.build.js +++ b/js/gutenberg/blocks.build.js @@ -1 +1 @@ -!function(e){function t(r){if(n[r])return n[r].exports;var o=n[r]={i:r,l:!1,exports:{}};return e[r].call(o.exports,o,o.exports,t),o.l=!0,o.exports}var n={};t.m=e,t.c=n,t.d=function(e,n,r){t.o(e,n)||Object.defineProperty(e,n,{configurable:!1,enumerable:!0,get:r})},t.n=function(e){var n=e&&e.__esModule?function(){return e.default}:function(){return e};return t.d(n,"a",n),n},t.o=function(e,t){return Object.prototype.hasOwnProperty.call(e,t)},t.p="",t(t.s=0)}([function(e,t,n){"use strict";Object.defineProperty(t,"__esModule",{value:!0});n(1)},function(e,t,n){"use strict";function r(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}function o(e,t){if(!e)throw new ReferenceError("this hasn't been initialised - super() hasn't been called");return!t||"object"!==typeof t&&"function"!==typeof t?e:t}function i(e,t){if("function"!==typeof t&&null!==t)throw new TypeError("Super expression must either be null or a function, not "+typeof t);e.prototype=Object.create(t&&t.prototype,{constructor:{value:e,enumerable:!1,writable:!0,configurable:!0}}),t&&(Object.setPrototypeOf?Object.setPrototypeOf(e,t):e.__proto__=t)}var l=n(2),s=(n.n(l),n(3)),a=(n.n(s),n(4)),u=n.n(a),p=function(){function e(e,t){for(var n=0;n'+n+"")})},"json")})},function(e,t){},function(e,t){},function(e,t){function n(e,t,n){switch(n.length){case 0:return e.call(t);case 1:return e.call(t,n[0]);case 2:return e.call(t,n[0],n[1]);case 3:return e.call(t,n[0],n[1],n[2])}return e.apply(t,n)}function r(e,t){for(var n=-1,r=Array(e);++n-1&&e%1==0&&e-1&&e%1==0&&e<=S}function d(e){var t=typeof e;return!!e&&("object"==t||"function"==t)}function g(e){return!!e&&"object"==typeof e}function w(e){return h(e)?o(e):l(e)}var S=9007199254740991,j="[object Arguments]",O="[object Function]",E="[object GeneratorFunction]",_=/^(?:0|[1-9]\d*)$/,I=Object.prototype,F=I.hasOwnProperty,C=I.toString,A=I.propertyIsEnumerable,k=function(e,t){return function(n){return e(t(n))}}(Object.keys,Object),D=Math.max,T=!A.call({valueOf:1},"valueOf"),x=Array.isArray,P=function(e){return s(function(t,n){var r=-1,o=n.length,i=o>1?n[o-1]:void 0,l=o>2?n[2]:void 0;for(i=e.length>3&&"function"==typeof i?(o--,i):void 0,l&&p(n[0],n[1],l)&&(i=o<3?void 0:i,o=1),t=Object(t);++r'+n+"")})},"json")})},function(e,t){},function(e,t){},function(e,t){function n(e,t,n){switch(n.length){case 0:return e.call(t);case 1:return e.call(t,n[0]);case 2:return e.call(t,n[0],n[1]);case 3:return e.call(t,n[0],n[1],n[2])}return e.apply(t,n)}function r(e,t){for(var n=-1,r=Array(e);++n-1&&e%1==0&&e-1&&e%1==0&&e<=S}function d(e){var t=typeof e;return!!e&&("object"==t||"function"==t)}function g(e){return!!e&&"object"==typeof e}function w(e){return h(e)?o(e):l(e)}var S=9007199254740991,j="[object Arguments]",O="[object Function]",E="[object GeneratorFunction]",_=/^(?:0|[1-9]\d*)$/,I=Object.prototype,F=I.hasOwnProperty,C=I.toString,A=I.propertyIsEnumerable,k=function(e,t){return function(n){return e(t(n))}}(Object.keys,Object),D=Math.max,T=!A.call({valueOf:1},"valueOf"),x=Array.isArray,P=function(e){return s(function(t,n){var r=-1,o=n.length,i=o>1?n[o-1]:void 0,l=o>2?n[2]:void 0;for(i=e.length>3&&"function"==typeof i?(o--,i):void 0,l&&p(n[0],n[1],l)&&(i=o<3?void 0:i,o=1),t=Object(t);++rloader = $loader; - $this->pluginDirUrl = plugin_dir_url($loader->get_plugin_main_file()); + $this->pluginDirUrl = plugin_dir_url( $loader->get_plugin_main_file() ); add_action( 'admin_menu', [ $this, 'add_settings_page' ] ); add_filter( 'manage_edit-ssc_group_columns', [ $this, 'ssc_group_columns' ] ); @@ -46,13 +47,80 @@ public function __construct(Plugin $loader) { * Get products from simple shop via API */ public function wp_ajax_load_simple_shop_products() { + $this->update_simpleshop_products_cache(); $products = $this->get_simpleshop_products(); - update_option( 'simpleshop_products', $products ); - echo wp_json_encode( $products ); - exit(); + wp_send_json( $products ); } + /** + * Return products. If you need force refresh products from API, call `update_simpleshop_products_cache()` before + * + * @return array + * @throws VyfakturujAPIException + */ public function get_simpleshop_products() { + $products = $this->get_simpleshop_products_cache(); + + if ( $products === null ) { + $this->update_simpleshop_products_cache(); + $products = $this->get_simpleshop_products_cache(); + } + + return $products; + } + + /** + * Returns current and valid products from cache or null if valid cache unavailable + * + * @return array|null + */ + protected function get_simpleshop_products_cache() { + $cache = get_option( 'ssc_cache_products', [] ); + $cacheKey = $this->loader->get_cache_user_key(); + + // Check if cache is exists & is valid + $cachedTime = isset( $cache[ $cacheKey ][ self::PRODUCTS_CACHE_FIELD ] ) ? (int) $cache[ $cacheKey ][ self::PRODUCTS_CACHE_FIELD ] : 0; + $age = time() - $cachedTime; + + if ( $age < self::PRODUCTS_CACHE_TTL ) { + $products = $cache[ $cacheKey ]; + unset( $products[ self::PRODUCTS_CACHE_FIELD ] ); + + return $products; + } + + return null; + } + + /** + * Update Products cache from Vyfakturuj API + * + * @throws VyfakturujAPIException + */ + public function update_simpleshop_products_cache() { + $products = $this->load_simpleshop_products(); + + $cacheKey = $this->loader->get_cache_user_key(); + $cachedTime = time(); + + $cache = [ + $cacheKey => array_merge( + $products, + [ self::PRODUCTS_CACHE_FIELD => $cachedTime ] + ) + ]; + + update_option( 'ssc_cache_products', $cache ); + } + + + /** + * Load products from Vyfakturuj API. Don't call method directly, use `get_simpleshop_products()` to use cache + * + * @return array + * @throws VyfakturujAPIException + */ + protected function load_simpleshop_products() { $values = []; if ( $this->loader->has_credentials() ) { $vyfakturuj_api = $this->loader->get_api_client(); diff --git a/src/Gutenberg.php b/src/Gutenberg.php index 7bd415d..c0a2ab5 100644 --- a/src/Gutenberg.php +++ b/src/Gutenberg.php @@ -31,14 +31,11 @@ public function __construct( Admin $admin, Group $group, Access $access, $plugin $this->pluginDirUrl = plugin_dir_url($pluginMainFile); } - function load_products() { - if ( ! get_option( 'simpleshop_products' ) ) { - update_option( 'simpleshop_products', $this->admin->get_simpleshop_products() ); - - } + public function load_products() { + return $this->admin->get_simpleshop_products(); } - function load_block_assets() { // phpcs:ignore + public function load_block_assets() { // phpcs:ignore // Register block styles for both frontend + backend. wp_register_style( @@ -61,7 +58,7 @@ function load_block_assets() { // phpcs:ignore 'simpleshop-gutenberg-block-js', 'ssGutenbergVariables', [ - 'products' => get_option( 'simpleshop_products', [] ), + 'products' => $this->load_products(), 'groups' => $this->group->get_groups(), ] ); @@ -99,6 +96,8 @@ function load_block_assets() { // phpcs:ignore * * @param $content * @param $block + * + * @return string */ public function maybe_hide_block( $content, $block ) { $args = [ diff --git a/src/Plugin.php b/src/Plugin.php index 51fecf2..fa0ca91 100644 --- a/src/Plugin.php +++ b/src/Plugin.php @@ -71,6 +71,15 @@ private function init() { new Cron( $this ); new Metaboxes( $this ); new Shortcodes($this->access); + $this->init_gutenberg(); + } + + private function init_gutenberg() { + if(function_exists('register_block_type') === false) { + // Skip init Gutenberg features - Gurenberg not supported in WP + return; + } + new Gutenberg($this->admin, $this->group, $this->access, $this->pluginMainFile); } @@ -90,6 +99,11 @@ protected function load_email() { return $this->settings->ssc_get_option( 'ssc_api_email' ); } + /** @return string|null Cache key related to API identity, or null when unlogged */ + public function get_cache_user_key() { + return $this->email ? md5( strtolower( $this->email ) ) : null; + } + public function get_api_email() { return $this->email; } diff --git a/src/Settings.php b/src/Settings.php index 79a02d9..6c665f7 100644 --- a/src/Settings.php +++ b/src/Settings.php @@ -172,7 +172,7 @@ public function add_options_page_metabox() { 'default' => '1', 'options' => [ '1' => __( 'Yes, send email to new member.', 'cmb2', 'simpleshop.cz', 'simpleshop-cz' ), - '2' => __( 'No, doesn\'t send email to new members.', 'cmb2', 'simpleshop.cz', 'simpleshop-cz' ), + '2' => __( 'No, don\'t send email to new members.', 'cmb2', 'simpleshop.cz', 'simpleshop-cz' ), ], ] );