Changeset 2762014
- Timestamp:
- 07/26/2022 09:57:48 PM (3 years ago)
- Location:
- spocket/trunk
- Files:
-
- 18 edited
-
composer.json (modified) (1 diff)
-
docker-compose.yml (modified) (2 diffs)
-
index.php (modified) (1 diff)
-
lib/AJAX.php (modified) (1 diff)
-
lib/AJAX/Requirements.php (modified) (2 diffs)
-
lib/AJAX/Spocket.php (modified) (2 diffs)
-
lib/ActivationCheck/Requirements.php (modified) (1 diff)
-
lib/Backend.php (modified) (1 diff)
-
lib/Backend/Assets.php (modified) (1 diff)
-
lib/Backend/Screen.php (modified) (1 diff)
-
lib/Common.php (modified) (1 diff)
-
lib/Common/Requirements.php (modified) (1 diff)
-
lib/Common/Spocket.php (modified) (1 diff)
-
lib/Plugin.php (modified) (1 diff)
-
lib/Webhook/AddCustomTopic.php (modified) (1 diff)
-
lib/Webhook/CurrencyUpdate.php (modified) (1 diff)
-
lib/Webhook/Data.php (modified) (1 diff)
-
readme.txt (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
-
spocket/trunk/composer.json
r2240220 r2762014 21 21 "Spocket\\": "lib" 22 22 } 23 }, 24 "config": { 25 "allow-plugins": { 26 "composer/installers": true, 27 "dealerdirect/phpcodesniffer-composer-installer": true 28 } 23 29 } 24 30 } -
spocket/trunk/docker-compose.yml
r2465231 r2762014 1 version: '3' 1 version: '3' 2 2 services: 3 3 db: … … 29 29 30 30 wordpress: 31 image: wordpress: 5.6.0-php7.4-apache31 image: wordpress:6.0.1-php7.4-apache 32 32 depends_on: 33 33 - db -
spocket/trunk/index.php
r2745504 r2762014 3 3 Plugin Name: Spocket 4 4 Description: Spocket - Dropshipping US/European Products 5 Version: 1.6. 85 Version: 1.6.9 6 6 Author: Spocket 7 7 Author URI: https://www.spocket.co -
spocket/trunk/lib/AJAX.php
r2164115 r2762014 1 1 <?php 2 2 3 /** 4 * File for Requirements class 5 * php version 7.4 6 * 7 * @category Category 8 * @package Spocket 9 * @author Display name <[email protected]> 10 * @license Copyright https://en.wikipedia.org/wiki/Copyright 11 * @link no link 12 */ 3 13 namespace Spocket; 4 14 5 15 /** 6 16 * Responsible for setting up AJAX functionality 17 * 18 * @category Category 19 * @package Spocket 20 * @author Display name <[email protected]> 21 * @license Copyright https://en.wikipedia.org/wiki/Copyright 22 * @link no link 7 23 */ 8 class AJAX { 24 class AJAX 25 { 9 26 10 /**11 * The plugin instance.12 *13 * @var Plugin14 */15 private $plugin;27 /** 28 * The plugin instance. 29 * 30 * @var Plugin 31 */ 32 private $_plugin; 16 33 17 /** 18 * Sets up initial instance properties 19 * 20 * @param \Spocket\Plugin $plugin This plugin's instance. 21 * @return void 22 */ 23 public function __construct( \Spocket\Plugin $plugin) { 24 $this->plugin = $plugin; 25 } 34 /** 35 * Sets up initial instance properties 36 * 37 * @param \Spocket\Plugin $plugin This plugin's instance. 38 * 39 * @return void 40 */ 41 public function __construct( \Spocket\Plugin $plugin) 42 { 43 $this->_plugin = $plugin; 44 } 26 45 27 /** 28 * Runs the class. 29 * 30 * @return void 31 */ 32 public function run() { 33 $AJAXRequirements = new \Spocket\AJAX\Requirements($this->plugin); 34 $AJAXRequirements->run(); 35 $AJAXSpocket = new \Spocket\AJAX\Spocket($this->plugin); 36 $AJAXSpocket->run(); 37 } 46 /** 47 * Runs the class. 48 * 49 * @return void 50 */ 51 public function run() 52 { 53 $AJAXRequirements = new \Spocket\AJAX\Requirements($this->_plugin); 54 $AJAXRequirements->run(); 55 $AJAXSpocket = new \Spocket\AJAX\Spocket($this->_plugin); 56 $AJAXSpocket->run(); 57 } 38 58 } -
spocket/trunk/lib/AJAX/Requirements.php
r2164115 r2762014 1 1 <?php 2 2 3 /** 4 * File for Requirements class 5 * php version 7.4 6 * 7 * @category Category 8 * @package Spocket\AJAX 9 * @author Display name <[email protected]> 10 * @license Copyright https://en.wikipedia.org/wiki/Copyright 11 * @link no link 12 */ 3 13 namespace Spocket\AJAX; 4 14 … … 7 17 /** 8 18 * Handles AJAX requests related to plugin requirements 19 * 20 * @category Category 21 * @package Spocket\AJAX 22 * @author Display name <[email protected]> 23 * @license Copyright https://en.wikipedia.org/wiki/Copyright 24 * @link no link 9 25 */ 10 class Requirements { 26 class Requirements 27 { 11 28 12 /**13 * The plugin instance.14 *15 * @var Plugin16 */17 private $plugin;29 /** 30 * The plugin instance. 31 * 32 * @var Plugin 33 */ 34 private $_plugin; 18 35 19 /** 20 * Sets up initial instance properties 21 * 22 * @param \Spocket\Plugin $plugin This plugin's instance. 23 * @return void 24 */ 25 public function __construct( \Spocket\Plugin $plugin) { 26 $this->plugin = $plugin; 27 } 36 /** 37 * Sets up initial instance properties 38 * 39 * @param \Spocket\Plugin $plugin This plugin's instance. 40 * 41 * @return void 42 */ 43 public function __construct( \Spocket\Plugin $plugin) 44 { 45 $this->_plugin = $plugin; 46 } 28 47 29 /** 30 * Gets requirements status. 31 * 32 * @return void 33 */ 34 public function getRequirementsStatus() { 35 check_ajax_referer('spocket-get-requirements-status-nonce', 'nonce'); 36 $requirementsStatus = CommonRequirements::getRequirementsStatus(); 37 wp_send_json_success(array( 38 'requirementsStatus' => $requirementsStatus, 39 )); 40 } 48 /** 49 * Gets requirements status. 50 * 51 * @return void 52 */ 53 public function getRequirementsStatus() 54 { 55 check_ajax_referer('spocket-get-requirements-status-nonce', 'nonce'); 56 $requirementsStatus = CommonRequirements::getRequirementsStatus(); 57 wp_send_json_success( 58 array( 59 'requirementsStatus' => $requirementsStatus, 60 ) 61 ); 62 } 41 63 42 /** 43 * Registeres and enqueues assets. 44 * 45 * @return void 46 */ 47 public function run() { 48 add_action('wp_ajax_spocket_requirements_status', array($this, 'getRequirementsStatus')); 49 } 64 /** 65 * Registeres and enqueues assets. 66 * 67 * @return void 68 */ 69 public function run() 70 { 71 add_action( 72 'wp_ajax_spocket_requirements_status', 73 [$this, 'getRequirementsStatus'] 74 ); 75 } 50 76 } -
spocket/trunk/lib/AJAX/Spocket.php
r2164115 r2762014 1 1 <?php 2 2 3 /** 4 * File for Spocket class 5 * php version 7.4 6 * 7 * @category Category 8 * @package Spocket\AJAX 9 * @author Display name <[email protected]> 10 * @license Copyright https://en.wikipedia.org/wiki/Copyright 11 * @link no link 12 */ 3 13 namespace Spocket\AJAX; 4 14 … … 7 17 /** 8 18 * Handles AJAX requests related to Spocket 19 * 20 * @category Category 21 * @package Spocket\AJAX 22 * @author Display name <[email protected]> 23 * @license Copyright https://en.wikipedia.org/wiki/Copyright 24 * @link no link 9 25 */ 10 class Spocket { 26 class Spocket 27 { 11 28 12 /**13 * The plugin instance.14 *15 * @var Plugin16 */17 private $plugin;29 /** 30 * The plugin instance. 31 * 32 * @var Plugin 33 */ 34 private $_plugin; 18 35 19 /**20 * Sets up initial instance properties21 *22 * @param \Spocket\Plugin $plugin This plugin's instance.23 * @return void24 */25 public function __construct( \Spocket\Plugin $plugin) {26 $this->plugin = $plugin;27 }28 36 29 /** 30 * Gets requirements status. 31 * 32 * @return void 33 */ 34 public function getSpocketStatus() { 35 check_ajax_referer('spocket-get-status-nonce', 'nonce'); 36 $spocketStatus = CommonSpocket::getSpocketStatus(); 37 wp_send_json_success(array( 38 'spocketStatus' => $spocketStatus, 39 )); 40 } 37 /** 38 * Sets up initial instance properties 39 * 40 * @param \Spocket\Plugin $plugin This plugin's instance. 41 * 42 * @return void 43 */ 44 public function __construct(\Spocket\Plugin $plugin) 45 { 46 $this->_plugin = $plugin; 41 47 42 /** 43 * Disconnects the store from Spocket. 44 * 45 * @return void 46 */ 47 public function disconnectSpocket() { 48 check_ajax_referer('spocket-disconnect-nonce', 'nonce'); 49 $disconnected = CommonSpocket::disconnectSpocket(); 50 wp_send_json_success(array( 51 'disconnected' => $disconnected, 52 )); 53 } 48 }//end __construct() 54 49 55 /**56 * Save shop url.57 *58 * @return void59 */60 public function saveShopUrl() {61 check_ajax_referer('spocket-save-shop-url-nonce', 'nonce');62 50 63 $spocketStatus = CommonSpocket::saveShopUrl(); 64 } 51 /** 52 * Gets requirements status. 53 * 54 * @return void 55 */ 56 public function getSpocketStatus() 57 { 58 check_ajax_referer('spocket-get-status-nonce', 'nonce'); 59 $spocketStatus = CommonSpocket::getSpocketStatus(); 60 wp_send_json_success( 61 array( 62 'spocketStatus' => $spocketStatus, 63 ) 64 ); 65 }//end getSpocketStatus() 65 66 66 /**67 * Finishes the direct signup process.68 *69 * @return void70 */71 public function finishDirectSignup() {72 check_ajax_referer('spocket-direct-signup-nonce', 'nonce');73 67 74 if (isset($_POST['storeAuthorizationKey'])) { 75 $storeAuthorizationKey = filter_var($_POST['storeAuthorizationKey'], FILTER_SANITIZE_STRING); 76 $isStoreAuthorizationKeyCreated = CommonSpocket::finishDirectSignup($storeAuthorizationKey); 68 /** 69 * Disconnects the store from Spocket. 70 * 71 * @return void 72 */ 73 public function disconnectSpocket() 74 { 75 check_ajax_referer('spocket-disconnect-nonce', 'nonce'); 76 $disconnected = CommonSpocket::disconnectSpocket(); 77 wp_send_json_success( 78 array( 79 'disconnected' => $disconnected, 80 ) 81 ); 82 }//end disconnectSpocket() 77 83 78 if ($isStoreAuthorizationKeyCreated) {79 return wp_send_json_success(array(80 'direct_signup_status' => 'created',81 ));82 }83 }84 wp_send_json_error(array(85 'direct_signup_status' => 'failed',86 'message' => 'Please provide your store authorization key'87 ));88 }89 84 90 public function removeStoreAuthorizationKeyFromRequest() { 91 check_ajax_referer('spocket-remove-store-authorization-key-nonce', 'nonce'); 92 update_option('spocket_store_authorization_key', '', false); 85 /** 86 * Save shop url. 87 * 88 * @return void 89 */ 90 public function saveShopUrl() 91 { 92 check_ajax_referer('spocket-save-shop-url-nonce', 'nonce'); 93 93 94 if (get_option('spocket_store_authorization_key') === '') { 95 return wp_send_json_success(array( 96 'status' => 'removed', 97 )); 98 } 99 } 94 $spocketStatus = CommonSpocket::saveShopUrl(); 100 95 101 /** 102 * Registeres and enqueues assets. 103 * 104 * @return void 105 */ 106 public function run() { 107 add_action('wp_ajax_spocket_status', array($this, 'getSpocketStatus')); 108 add_action('wp_ajax_spocket_disconnect', array($this, 'disconnectSpocket')); 109 add_action('wp_ajax_spocket_save_shop_url', array($this, 'saveShopUrl')); 110 add_action('wp_ajax_spocket_direct_signup', array($this, 'finishDirectSignup')); 111 add_action( 112 'wp_ajax_spocket_remove_store_authorization_key_from_request', 113 array($this, 'removeStoreAuthorizationKeyFromRequest') 114 ); 115 } 116 } 96 }//end saveShopUrl() 97 98 99 /** 100 * Finishes the direct signup process. 101 * 102 * @return void 103 */ 104 public function finishDirectSignup() 105 { 106 check_ajax_referer('spocket-direct-signup-nonce', 'nonce'); 107 108 if (isset($_POST['storeAuthorizationKey'])) { 109 $storeAuthorizationKey = sanitize_text_field( 110 $_POST['storeAuthorizationKey'] 111 ); 112 $isStoreAuthorizationKeyCreated = CommonSpocket::finishDirectSignup( 113 $storeAuthorizationKey 114 ); 115 116 if ($isStoreAuthorizationKeyCreated) { 117 return wp_send_json_success(['direct_signup_status' => 'created']); 118 } 119 } 120 121 wp_send_json_error( 122 [ 123 'direct_signup_status' => 'failed', 124 'message' => 'Please provide your store authorization key' 125 ] 126 ); 127 128 }//end finishDirectSignup() 129 130 131 /** 132 * Some comment. 133 * 134 * @return void 135 */ 136 public function removeStoreAuthorizationKeyFromRequest() 137 { 138 check_ajax_referer('spocket-remove-store-authorization-key-nonce', 'nonce'); 139 update_option('spocket_store_authorization_key', '', false); 140 141 if (get_option('spocket_store_authorization_key') === '') { 142 return wp_send_json_success(['status' => 'removed']); 143 } 144 145 }//end removeStoreAuthorizationKeyFromRequest() 146 147 148 /** 149 * Registeres and enqueues assets. 150 * 151 * @return void 152 */ 153 public function run() 154 { 155 add_action('wp_ajax_spocket_status', [$this, 'getSpocketStatus']); 156 add_action('wp_ajax_spocket_disconnect', [$this, 'disconnectSpocket']); 157 add_action('wp_ajax_spocket_save_shop_url', [$this, 'saveShopUrl']); 158 add_action('wp_ajax_spocket_direct_signup', [$this, 'finishDirectSignup']); 159 add_action( 160 'wp_ajax_spocket_remove_store_authorization_key_from_request', 161 [$this, 'removeStoreAuthorizationKeyFromRequest'] 162 ); 163 164 }//end run() 165 166 167 }//end class -
spocket/trunk/lib/ActivationCheck/Requirements.php
r2240220 r2762014 1 1 <?php 2 2 3 /** 4 * File for Requirements class 5 * php version 7.4 6 * 7 * @category Category 8 * @package Spocket 9 * @author Display name <[email protected]> 10 * @license Copyright https://en.wikipedia.org/wiki/Copyright 11 * @link no link 12 */ 3 13 namespace Spocket\ActivationCheck; 4 14 5 class Requirements { 15 /** 16 * Requirements 17 * 18 * @category Category 19 * @package Spocket 20 * @author Display name <[email protected]> 21 * @license Copyright https://en.wikipedia.org/wiki/Copyright 22 * @link no link 23 */ 24 class Requirements 25 { 6 26 7 private static $object = null;27 private static $_object = null; 8 28 9 /**10 * The plugin instance.11 *12 * @var Plugin13 */14 private $plugin;29 /** 30 * The plugin instance. 31 * 32 * @var Plugin 33 */ 34 private $_plugin; 15 35 16 /**17 * The response instance.18 *19 * @var response20 */21 private $response;36 /** 37 * The response instance. 38 * 39 * @var response 40 */ 41 private $_response; 22 42 23 /** 24 * Verify if WooCommerce is installed and activated. 25 * 26 * @return bool 27 */ 28 private function isWoocommerceActivated() { 29 if (is_plugin_active('woocommerce/woocommerce.php')) { 30 return true; 31 } 43 /** 44 * Verify if WooCommerce is installed and activated. 45 * 46 * @return bool 47 */ 48 private function _isWoocommerceActivated() 49 { 50 if (is_plugin_active('woocommerce/woocommerce.php')) { 51 return true; 52 } 32 53 33 return false;34 }54 return false; 55 } 35 56 36 /** 37 * Disable plugin. 38 * 39 * @return void 40 */ 41 private function deactivateSpocketPlugin() { 42 if (current_user_can('activate_plugins') && is_plugin_active(self::$object->plugin->getPluginBaseName())) { 43 deactivate_plugins(plugin_basename(self::$object->plugin->getPluginBaseName())); 44 if ( isset( $_GET['activate'] ) ) { 45 unset( $_GET['activate'] ); 46 } 47 } 48 } 57 /** 58 * Disable plugin. 59 * 60 * @return void 61 */ 62 private function _deactivateSpocketPlugin() 63 { 64 if (current_user_can('activate_plugins') 65 && is_plugin_active(self::$_object->_plugin->getPluginBaseName()) 66 ) { 67 deactivate_plugins( 68 plugin_basename( 69 self::$_object->_plugin->getPluginBaseName() 70 ) 71 ); 72 if (isset($_GET['activate']) ) { 73 unset($_GET['activate']); 74 } 75 } 76 } 49 77 50 /** 51 * Sets up initial instance properties 52 * 53 * @param \Spocket\Plugin $plugin This plugin's instance. 54 * @return void 55 */ 56 public function __construct( \Spocket\Plugin $plugin) { 57 self::$object = $this; 58 $this->plugin = $plugin; 59 } 78 /** 79 * Sets up initial instance properties 80 * 81 * @param \Spocket\Plugin $plugin This plugin's instance. 82 * 83 * @return void 84 */ 85 public function __construct( \Spocket\Plugin $plugin) 86 { 87 self::$_object = $this; 88 $this->_plugin = $plugin; 89 } 60 90 61 /** 62 * Disable the plugin if the request fails. 63 * 64 * @return void 65 */ 66 public static function disablePluginIfRequirementsFail() { 67 if (!self::$object->isWoocommerceActivated()) { 68 self::$object->deactivateSpocketPlugin(); 69 add_action( 70 'admin_head', 71 function() { 72 self::notifyUser('Spocket Plugin', 'WooCoommerce needs to be installed and activated before you can activate spocket plugin.'); 73 } 74 ); 75 } 76 } 91 /** 92 * Disable the plugin if the request fails. 93 * 94 * @return void 95 */ 96 public static function disablePluginIfRequirementsFail() 97 { 98 if (!self::$_object->_isWoocommerceActivated()) { 99 self::$_object->_deactivateSpocketPlugin(); 100 add_action( 101 'admin_head', 102 function () { 103 self::notifyUser( 104 'Spocket Plugin', 105 'WooCoommerce needs to be installed and activated before '. 106 'you can activate spocket plugin.' 107 ); 108 } 109 ); 110 } 111 } 77 112 78 /** 79 * Shows up a notice error message on WordPress Dashboard. 80 * 81 * @param string $title The message's title 82 * @param string $message the message itself 83 * 84 * @return void 85 */ 86 public static function notifyUser( $title, $message) { 87 echo '<div class="error"> 113 /** 114 * Shows up a notice error message on WordPress Dashboard. 115 * 116 * @param string $title The message's title 117 * @param string $message the message itself 118 * 119 * @return void 120 */ 121 public static function notifyUser( $title, $message) 122 { 123 echo '<div class="error"> 88 124 <p>' . 89 esc_html__($title)90 . '</p>125 esc_html__($title) 126 . '</p> 91 127 <p> 92 128 <b>Error: </b>' . esc_html__($message) 93 . '</p>129 . '</p> 94 130 </div>'; 95 }131 } 96 132 97 /** 98 * Runs the requirements. 99 * 100 * @return void 101 */ 102 public function run() { 103 add_action('admin_init', array($this, 'disablePluginIfRequirementsFail')); 104 } 133 /** 134 * Runs the requirements. 135 * 136 * @return void 137 */ 138 public function run() 139 { 140 add_action('admin_init', array($this, 'disablePluginIfRequirementsFail')); 141 } 105 142 106 143 } -
spocket/trunk/lib/Backend.php
r2164115 r2762014 1 1 <?php 2 2 3 /** 4 * File for Requirements class 5 * php version 7.4 6 * 7 * @category Category 8 * @package Spocket 9 * @author Display name <[email protected]> 10 * @license Copyright https://en.wikipedia.org/wiki/Copyright 11 * @link no link 12 */ 3 13 namespace Spocket; 4 14 5 15 /** 6 16 * Responsible for setting up backend functionality 17 * 18 * @category Category 19 * @package Spocket 20 * @author Display name <[email protected]> 21 * @license Copyright https://en.wikipedia.org/wiki/Copyright 22 * @link no link 7 23 */ 8 class Backend { 24 class Backend 25 { 9 26 10 /**11 * The plugin instance.12 *13 * @var Plugin14 */15 private $plugin;27 /** 28 * The plugin instance. 29 * 30 * @var Plugin 31 */ 32 private $_plugin; 16 33 17 /** 18 * Sets up initial instance properties 19 * 20 * @param \Spocket\Plugin $plugin This plugin's instance. 21 * @return void 22 */ 23 public function __construct( \Spocket\Plugin $plugin) { 24 $this->plugin = $plugin; 25 } 34 /** 35 * Sets up initial instance properties 36 * 37 * @param \Spocket\Plugin $plugin This plugin's instance. 38 * 39 * @return void 40 */ 41 public function __construct( \Spocket\Plugin $plugin) 42 { 43 $this->_plugin = $plugin; 44 } 26 45 27 /** 28 * Runs the class. 29 * 30 * @return void 31 */ 32 public function run() { 33 $BackendAssets = new \Spocket\Backend\Assets($this->plugin); 34 $BackendAssets->run(); 35 $BackendScreen = new \Spocket\Backend\Screen($this->plugin); 36 $BackendScreen->run(); 37 } 46 /** 47 * Runs the class. 48 * 49 * @return void 50 */ 51 public function run() 52 { 53 $BackendAssets = new \Spocket\Backend\Assets($this->_plugin); 54 $BackendAssets->run(); 55 $BackendScreen = new \Spocket\Backend\Screen($this->_plugin); 56 $BackendScreen->run(); 57 } 38 58 } -
spocket/trunk/lib/Backend/Assets.php
r2164115 r2762014 1 1 <?php 2 2 3 /** 4 * File for Requirements class 5 * php version 7.4 6 * 7 * @category Category 8 * @package Spocket\Backend 9 * @author Display name <[email protected]> 10 * @license Copyright https://en.wikipedia.org/wiki/Copyright 11 * @link no link 12 */ 3 13 namespace Spocket\Backend; 4 14 5 15 /** 6 16 * Responsible for registering and enqueueing assets on the Backend 17 * 18 * @category Category 19 * @package Spocket\Backend 20 * @author Display name <[email protected]> 21 * @license Copyright https://en.wikipedia.org/wiki/Copyright 22 * @link no link 7 23 */ 8 class Assets { 24 class Assets 25 { 9 26 10 /**11 * The plugin instance.12 *13 * @var Plugin14 */15 private $plugin;27 /** 28 * The plugin instance. 29 * 30 * @var Plugin 31 */ 32 private $_plugin; 16 33 17 /** 18 * Sets up initial instance properties 19 * 20 * @param \Spocket\Plugin $plugin This plugin's instance. 21 * @return void 22 */ 23 public function __construct( \Spocket\Plugin $plugin) { 24 $this->plugin = $plugin; 25 } 34 /** 35 * Sets up initial instance properties 36 * 37 * @param \Spocket\Plugin $plugin This plugin's instance. 38 * 39 * @return void 40 */ 41 public function __construct( \Spocket\Plugin $plugin) 42 { 43 $this->_plugin = $plugin; 44 } 26 45 27 /** 28 * Defines a list of strings used in the interface, localized and passed 29 * to JavaScript. 30 * 31 * @return array 32 */ 33 public function l10nList() { 34 return array( 35 'test' => __( 36 'Test', 37 'spocket' 38 ), 39 ); 40 } 46 /** 47 * Defines a list of strings used in the interface, localized and passed 48 * to JavaScript. 49 * 50 * @return array 51 */ 52 public function l10nList() 53 { 54 return array( 55 'test' => __( 56 'Test', 57 'spocket' 58 ), 59 ); 60 } 41 61 42 /** 43 * Enqueues the plugin script. 44 * 45 * @param string $hook The current admin page. 46 * @return void 47 */ 48 public function enqueueScript( $hook) { 49 if ('toplevel_page_spocket' !== $hook) { 50 return; 51 } 62 /** 63 * Enqueues the plugin script. 64 * 65 * @param string $hook The current admin page. 66 * 67 * @return void 68 */ 69 public function enqueueScript( $hook) 70 { 71 if ('toplevel_page_spocket' !== $hook) { 72 return; 73 } 52 74 53 wp_enqueue_script(54 'spocket',55 "{$this->plugin->getAssetsURL()}js/admin.js",56 array(),57 $this->plugin->getVersion()58 );75 wp_enqueue_script( 76 'spocket', 77 "{$this->_plugin->getAssetsURL()}js/admin.js", 78 array(), 79 $this->_plugin->getVersion() 80 ); 59 81 60 $scriptData = array( 61 'ajaxUrl' => admin_url('admin-ajax.php'), 62 'nonces' => array( 63 'getRequirementsStatus' => wp_create_nonce('spocket-get-requirements-status-nonce'), 64 'getSpocketStatus' => wp_create_nonce('spocket-get-status-nonce'), 65 'disconnectSpocketNonce' => wp_create_nonce('spocket-disconnect-nonce'), 66 'saveShopUrlNonce' => wp_create_nonce('spocket-save-shop-url-nonce'), 67 'directSignup' => wp_create_nonce('spocket-direct-signup-nonce'), 68 'removeStoreAuthorizationKeyFromRequest' => wp_create_nonce('spocket-remove-store-authorization-key-nonce'), 69 ), 70 'l10n' => $this->l10nList(), 71 'assetsSrcUrl' => $this->plugin->getAssetsSrcURL(), 72 'assetsUrl' => $this->plugin->getAssetsURL(), 73 'storeUrl' => get_site_url(), 74 'spocketShopUrl' => get_option('spocket_shop_url', ''), 75 'spocketAdminUrl' => get_admin_url(null, 'admin.php?page=spocket'), 76 'spocketAuthToken' => get_option('spocket_auth_token', ''), 77 'spocketUserId' => get_option('spocket_user_id', '') 78 ); 82 $scriptData = array( 83 'ajaxUrl' => admin_url('admin-ajax.php'), 84 'nonces' => array( 85 'getRequirementsStatus' => wp_create_nonce( 86 'spocket-get-requirements-status-nonce' 87 ), 88 'getSpocketStatus' => wp_create_nonce('spocket-get-status-nonce'), 89 'disconnectSpocketNonce' => wp_create_nonce('spocket-disconnect-nonce'), 90 'saveShopUrlNonce' => wp_create_nonce('spocket-save-shop-url-nonce'), 91 'directSignup' => wp_create_nonce('spocket-direct-signup-nonce'), 92 'removeStoreAuthorizationKeyFromRequest' => wp_create_nonce( 93 'spocket-remove-store-authorization-key-nonce' 94 ), 95 ), 96 'l10n' => $this->l10nList(), 97 'assetsSrcUrl' => $this->_plugin->getAssetsSrcURL(), 98 'assetsUrl' => $this->_plugin->getAssetsURL(), 99 'storeUrl' => get_site_url(), 100 'spocketShopUrl' => get_option('spocket_shop_url', ''), 101 'spocketAdminUrl' => get_admin_url(null, 'admin.php?page=spocket'), 102 'spocketAuthToken' => get_option('spocket_auth_token', ''), 103 'spocketUserId' => get_option('spocket_user_id', '') 104 ); 79 105 80 wp_localize_script(81 'spocket',82 'SpocketData',83 $scriptData84 );85 }106 wp_localize_script( 107 'spocket', 108 'SpocketData', 109 $scriptData 110 ); 111 } 86 112 87 /** 88 * Enqueue all styles needed on the Backend. 89 * 90 * @param string $hook The current admin page. 91 * @return void 92 */ 93 public function enqueueStyles( $hook) { 94 if ('toplevel_page_spocket'!== $hook) { 95 return; 96 } 113 /** 114 * Enqueue all styles needed on the Backend. 115 * 116 * @param string $hook The current admin page. 117 * 118 * @return void 119 */ 120 public function enqueueStyles( $hook) 121 { 122 if ('toplevel_page_spocket'!== $hook) { 123 return; 124 } 97 125 98 if (\getenv('IS_DEV') === 'true') {99 // Styles are live-reloaded with webpack JS100 return;101 }126 if (\getenv('IS_DEV') === 'true') { 127 // Styles are live-reloaded with webpack JS 128 return; 129 } 102 130 103 \wp_enqueue_style(104 'spocket',105 "{$this->plugin->getAssetsURL()}css/admin.css",106 array(),107 $this->plugin->getVersion()108 );109 }131 \wp_enqueue_style( 132 'spocket', 133 "{$this->_plugin->getAssetsURL()}css/admin.css", 134 array(), 135 $this->_plugin->getVersion() 136 ); 137 } 110 138 111 public function enqueueTopLevelMenuStyle() { 112 \wp_enqueue_style( 113 'spocket', 114 "{$this->plugin->getAssetsURL()}css/toplevel-menu.css", 115 array(), 116 $this->plugin->getVersion() 117 ); 118 } 139 /** 140 * EnqueueTopLevelMenuStyle. 141 * 142 * @return void 143 */ 144 public function enqueueTopLevelMenuStyle() 145 { 146 \wp_enqueue_style( 147 'spocket', 148 "{$this->_plugin->getAssetsURL()}css/toplevel-menu.css", 149 array(), 150 $this->_plugin->getVersion() 151 ); 152 } 119 153 120 /** 121 * Registeres and enqueues assets. 122 * 123 * @return void 124 */ 125 public function run() { 126 add_action('admin_enqueue_scripts', array($this, 'enqueueScript')); 127 add_action('admin_enqueue_scripts', array($this, 'enqueueStyles')); 128 add_action('admin_enqueue_scripts', array($this, 'enqueueTopLevelMenuStyle')); 129 } 154 /** 155 * Registeres and enqueues assets. 156 * 157 * @return void 158 */ 159 public function run() 160 { 161 add_action('admin_enqueue_scripts', array($this, 'enqueueScript')); 162 add_action('admin_enqueue_scripts', array($this, 'enqueueStyles')); 163 add_action( 164 'admin_enqueue_scripts', 165 array($this, 'enqueueTopLevelMenuStyle') 166 ); 167 } 130 168 } -
spocket/trunk/lib/Backend/Screen.php
r2164115 r2762014 1 1 <?php 2 2 3 /** 4 * File for Requirements class 5 * php version 7.4 6 * 7 * @category Category 8 * @package Spocket\Backend 9 * @author Display name <[email protected]> 10 * @license Copyright https://en.wikipedia.org/wiki/Copyright 11 * @link no link 12 */ 3 13 namespace Spocket\Backend; 4 14 5 15 /** 6 16 * Displays an admin interface for the plugin 17 * 18 * @category Category 19 * @package Spocket\Backend 20 * @author Display name <[email protected]> 21 * @license Copyright https://en.wikipedia.org/wiki/Copyright 22 * @link no link 7 23 */ 8 class Screen { 24 class Screen 25 { 9 26 10 /**11 * The plugin instance.12 *13 * @var Plugin14 */15 private $plugin;27 /** 28 * The plugin instance. 29 * 30 * @var Plugin 31 */ 32 private $_plugin; 16 33 17 /** 18 * Sets up initial instance properties 19 * 20 * @param \Spocket\Plugin $plugin This plugin's instance. 21 * @return void 22 */ 23 public function __construct( \Spocket\Plugin $plugin) { 24 $this->plugin = $plugin; 25 } 34 /** 35 * Sets up initial instance properties 36 * 37 * @param \Spocket\Plugin $plugin This plugin's instance. 38 * 39 * @return void 40 */ 41 public function __construct( \Spocket\Plugin $plugin) 42 { 43 $this->_plugin = $plugin; 44 } 26 45 27 /** 28 * Registers interface, to be displayed as a sub-menu of the Settings menu 29 * item. 30 * 31 * @return void 32 */ 33 public function addMenuPage() { 34 add_menu_page( 35 esc_html__('Spocket', 'spocket'), 36 esc_html__('Spocket', 'spocket'), 37 'manage_woocommerce', 38 'spocket', 39 array($this, 'displayInterface'), 40 "{$this->plugin->getAssetsURL()}images/white-logo.png", 41 57 42 ); 43 } 46 /** 47 * Registers interface, to be displayed as a sub-menu of the Settings menu 48 * item. 49 * 50 * @return void 51 */ 52 public function addMenuPage() 53 { 54 add_menu_page( 55 esc_html__('Spocket', 'spocket'), 56 esc_html__('Spocket', 'spocket'), 57 'manage_woocommerce', 58 'spocket', 59 array($this, 'displayInterface'), 60 "{$this->_plugin->getAssetsURL()}images/white-logo.png", 61 57 62 ); 63 } 44 64 45 /** 46 * Displays an interface. 47 * 48 * @return void 49 */ 50 public function displayInterface() { 51 do_action('spocket_before_interface', $this); 52 echo '<div class="wrap js-spocket-admin-interface">'; 53 esc_html_e('Loading, please wait...', 'spocket'); 54 echo '</div>'; 55 do_action('spocket_after_interface', $this); 56 } 65 /** 66 * Displays an interface. 67 * 68 * @return void 69 */ 70 public function displayInterface() 71 { 72 do_action('spocket_before_interface', $this); 73 echo '<div class="wrap js-spocket-admin-interface">'; 74 esc_html_e('Loading, please wait...', 'spocket'); 75 echo '</div>'; 76 do_action('spocket_after_interface', $this); 77 } 57 78 58 /** 59 * Registers filters and actions. 60 * 61 * @return void 62 */ 63 public function run() { 64 add_action('admin_menu', array($this, 'addMenuPage')); 65 } 79 /** 80 * Registers filters and actions. 81 * 82 * @return void 83 */ 84 public function run() 85 { 86 add_action('admin_menu', array($this, 'addMenuPage')); 87 } 66 88 } -
spocket/trunk/lib/Common.php
r2164115 r2762014 1 1 <?php 2 2 3 /** 4 * File for Requirements class 5 * php version 7.4 6 * 7 * @category Category 8 * @package Spocket 9 * @author Display name <[email protected]> 10 * @license Copyright https://en.wikipedia.org/wiki/Copyright 11 * @link no link 12 */ 3 13 namespace Spocket; 4 14 5 15 /** 6 16 * Responsible for setting up functionality on both frontend and the backend 17 * 18 * @category Category 19 * @package Spocket 20 * @author Display name <[email protected]> 21 * @license Copyright https://en.wikipedia.org/wiki/Copyright 22 * @link no link 7 23 */ 8 class Common { 24 class Common 25 { 9 26 10 /**11 * The plugin instance.12 *13 * @var Plugin14 */15 private $plugin;27 /** 28 * The plugin instance. 29 * 30 * @var Plugin 31 */ 32 private $_plugin; 16 33 17 /** 18 * Sets up initial instance properties 19 * 20 * @param \Spocket\Plugin $plugin This plugin's instance. 21 * @return void 22 */ 23 public function __construct( \Spocket\Plugin $plugin) { 24 $this->plugin = $plugin; 25 } 34 /** 35 * Sets up initial instance properties 36 * 37 * @param \Spocket\Plugin $plugin This plugin's instance. 38 * 39 * @return void 40 */ 41 public function __construct( \Spocket\Plugin $plugin) 42 { 43 $this->_plugin = $plugin; 44 } 26 45 27 /** 28 * Runs the class. 29 * 30 * @return void 31 */ 32 public function run() { 33 $CommonSpocket = new \Spocket\Common\Spocket($this->plugin); 34 $CommonSpocket->run(); 35 } 46 /** 47 * Runs the class. 48 * 49 * @return void 50 */ 51 public function run() 52 { 53 $CommonSpocket = new \Spocket\Common\Spocket($this->_plugin); 54 $CommonSpocket->run(); 55 } 36 56 } -
spocket/trunk/lib/Common/Requirements.php
r2327792 r2762014 1 1 <?php 2 2 3 /** 4 * File for Requirements class 5 * php version 7.4 6 * 7 * @category Category 8 * @package Spocket\Common 9 * @author Display name <[email protected]> 10 * @license Copyright https://en.wikipedia.org/wiki/Copyright 11 * @link no link 12 */ 3 13 namespace Spocket\Common; 4 14 5 15 /** 6 16 * Handles requirements checks 17 * 18 * @category Category 19 * @package Spocket\Common 20 * @author Display name <[email protected]> 21 * @license Copyright https://en.wikipedia.org/wiki/Copyright 22 * @link no link 7 23 */ 8 class Requirements { 9 10 /** 11 * Checks if cURL is activated on php installation. 12 * 13 * @return array cURL requirements check results 14 */ 15 public static function checkCurl() { 16 if (!in_array('curl', get_loaded_extensions())) { 17 return array( 18 'title' => __('PHP cURL', 'spocket'), 19 'pass' => false, 20 'reason' => __('PHP cURL seems to be disabled or not installed on your server.', 'spocket'), 21 'solution' => __('Please activate cURL to use the spocket plugin. <br>') 22 ); 23 } 24 25 return array( 26 'title' => __('PHP cURL', 'spocket'), 27 'pass' => true, 28 ); 29 } 30 31 /** 32 * Checks if the connection is SSL. 33 * 34 * @return array SSL requirements check results 35 */ 36 public static function checkSSLConnection() { 37 if (!getenv('IS_DEV') && ( !isset($_SERVER['HTTPS']) || empty($_SERVER['HTTPS']) || 'on' !== strtolower($_SERVER['HTTPS']) )) { 38 return array( 39 'title' => __('SSL Connection', 'spocket'), 40 'pass' => false, 41 'reason' => __('You are not using a SSL connection', 'spocket'), 42 'solution' => __( 43 'Please set up a HTTPS certificate to use Spocket.<br>' 44 ), 45 ); 46 } 47 48 return array( 49 'title' => __('SSL Connection', 'spocket'), 50 'pass' => true, 51 ); 52 } 53 54 /** 55 * Checks Store name. 56 * checks if the store has a name to use on spocket. 57 * 58 * @return array Store name requirements check results 59 */ 60 public static function getStoreNameRequirementsStatus() { 61 62 if (get_option('blogname') === '') { 63 return array( 64 'title' => __('Store name', 'spocket'), 65 'pass' => false, 66 'reason' => __('Seems like your store does not have a name.', 'spocket'), 67 'solution' => __( 68 'Please set a name for your store. <br> You can set one in <b>Settings > General > Site Title<b>', 69 'spocket' 70 ), 71 ); 72 } 73 74 return array( 75 'title' => __('Store name', 'spocket'), 76 'pass' => true, 77 ); 78 } 79 80 /** 81 * Checks Current User requirements. 82 * 83 * @return array Current User requirements check results 84 */ 85 public static function getUserRequirementsStatus() { 86 $currentUser = wp_get_current_user(); 87 if ($currentUser->get('user_email') === '') { 88 return array( 89 'title' => __('Current User', 'spocket'), 90 'pass' => false, 91 'reason' => __('Your user does not have an e-mail account.', 'spocket'), 92 'solution' => __( 93 'Please you need to assign an e-mail to your user.', 94 'spocket' 95 ), 96 ); 97 } 98 99 return array( 100 'title' => __('Current User', 'spocket'), 101 'pass' => true, 102 ); 103 } 104 105 /** 106 * Checks WooCommerce requirements. 107 * 108 * @return array WooCommerce requirements check results 109 */ 110 public static function getWooCommerceRequirementsStatus() { 111 if (!function_exists('wc')) { 112 return array( 113 'title' => __('WooCommerce', 'spocket'), 114 'pass' => false, 115 'reason' => __('WooCommerce plugin is not activated', 'spocket'), 116 'solution' => __( 117 'Please install and activate the WooCommerce plugin', 118 'spocket' 119 ), 120 ); 121 } 122 123 if (!version_compare(wc()->version, '2.6', '>=')) { 124 return array( 125 'title' => __('WooCommerce', 'spocket'), 126 'pass' => false, 127 'reason' => __( 128 'WooCommerce plugin is out of date, version >= 2.6 is required', 129 'spocket' 130 ), 131 'solution' => __( 132 'Please update your WooCommerce plugin', 133 'spocket' 134 ), 135 ); 136 } 137 138 return array( 139 'title' => __('WooCommerce', 'spocket'), 140 'pass' => true, 141 ); 142 } 143 144 /** 145 * Checks WordPress requirements. 146 * 147 * @return array WordPress requirements check results 148 */ 149 public static function getWordPressRequirementsStatus() { 150 if (!version_compare(get_bloginfo('version'), '4.4', '>=')) { 151 return array( 152 'title' => __('WordPress', 'spocket'), 153 'pass' => false, 154 'reason' => __( 155 'WordPress is out of date, version >= 4.4 is required', 156 'spocket' 157 ), 158 'solution' => __( 159 'Please update your WordPress installation', 160 'spocket' 161 ), 162 ); 163 } 164 165 return array( 166 'title' => __('WordPress', 'spocket'), 167 'pass' => true, 168 ); 169 } 170 171 /** 172 * Checks permalinks requirements. 173 * 174 * @return array Permalinks requirements check results 175 */ 176 public static function getPermalinksRequirementsStatus() { 177 if (get_option('permalink_structure', '') === '') { 178 return array( 179 'title' => __('Permalinks', 'spocket'), 180 'pass' => false, 181 'reason' => __( 182 'Permalinks set to "Plain"', 183 'spocket' 184 ), 185 'solution' => __( 186 'Set permalinks to anything other than "Plain" in <b>Settings > Permalinks</b>', 187 'spocket' 188 ), 189 ); 190 } 191 192 return array( 193 'title' => __('Permalinks', 'spocket'), 194 'pass' => true, 195 ); 196 } 197 198 /** 199 * Retrieves collective requirements status. 200 * 201 * @return array Requirements check results 202 */ 203 public static function getRequirementsStatus() { 204 $requirementsStatus = array( 205 'woocommerce' => self::getWooCommerceRequirementsStatus(), 206 'wordpress' => self::getWordPressRequirementsStatus(), 207 'permalinks' => self::getPermalinksRequirementsStatus(), 208 'user' => self::getUserRequirementsStatus(), 209 'store_name' => self::getStoreNameRequirementsStatus(), 210 'check_ssl' => self::checkSSLConnection(), 211 'check_curl' => self::checkCurl(), 212 ); 213 return $requirementsStatus; 214 } 215 216 private static function getApiURL() { 217 $plugin = new \Spocket\Plugin(); 218 return $plugin->getApiURL(); 219 } 24 class Requirements 25 { 26 27 /** 28 * Checks if cURL is activated on php installation. 29 * 30 * @return array cURL requirements check results 31 */ 32 public static function checkCurl() 33 { 34 if (!in_array('curl', get_loaded_extensions())) { 35 return array( 36 'title' => __('PHP cURL', 'spocket'), 37 'pass' => false, 38 'reason' => __( 39 'PHP cURL seems to be disabled or not installed on your server.', 40 'spocket' 41 ), 42 'solution' => __('Please activate cURL to use the spocket plugin. <br>') 43 ); 44 } 45 46 return array( 47 'title' => __('PHP cURL', 'spocket'), 48 'pass' => true, 49 ); 50 } 51 52 /** 53 * Checks if the connection is SSL. 54 * 55 * @return array SSL requirements check results 56 */ 57 public static function checkSSLConnection() 58 { 59 if (!getenv('IS_DEV') 60 && ( !isset($_SERVER['HTTPS']) 61 || empty($_SERVER['HTTPS']) 62 || 'on' !== strtolower($_SERVER['HTTPS']) ) 63 ) { 64 return array( 65 'title' => __('SSL Connection', 'spocket'), 66 'pass' => false, 67 'reason' => __('You are not using a SSL connection', 'spocket'), 68 'solution' => __( 69 'Please set up a HTTPS certificate to use Spocket.<br>' 70 ), 71 ); 72 } 73 74 return array( 75 'title' => __('SSL Connection', 'spocket'), 76 'pass' => true, 77 ); 78 } 79 80 /** 81 * Checks Store name. 82 * checks if the store has a name to use on spocket. 83 * 84 * @return array Store name requirements check results 85 */ 86 public static function getStoreNameRequirementsStatus() 87 { 88 89 if (get_option('blogname') === '') { 90 return array( 91 'title' => __('Store name', 'spocket'), 92 'pass' => false, 93 'reason' => __('Seems like your store does not have a name.', 'spocket'), 94 'solution' => __( 95 'Please set a name for your store. <br> You can set one '. 96 'in <b>Settings > General > Site Title<b>', 97 'spocket' 98 ), 99 ); 100 } 101 102 return array( 103 'title' => __('Store name', 'spocket'), 104 'pass' => true, 105 ); 106 } 107 108 /** 109 * Checks Current User requirements. 110 * 111 * @return array Current User requirements check results 112 */ 113 public static function getUserRequirementsStatus() 114 { 115 $currentUser = wp_get_current_user(); 116 if ($currentUser->get('user_email') === '') { 117 return array( 118 'title' => __('Current User', 'spocket'), 119 'pass' => false, 120 'reason' => __('Your user does not have an e-mail account.', 'spocket'), 121 'solution' => __( 122 'Please you need to assign an e-mail to your user.', 123 'spocket' 124 ), 125 ); 126 } 127 128 return array( 129 'title' => __('Current User', 'spocket'), 130 'pass' => true, 131 ); 132 } 133 134 /** 135 * Checks WooCommerce requirements. 136 * 137 * @return array WooCommerce requirements check results 138 */ 139 public static function getWooCommerceRequirementsStatus() 140 { 141 if (!function_exists('wc')) { 142 return array( 143 'title' => __('WooCommerce', 'spocket'), 144 'pass' => false, 145 'reason' => __('WooCommerce plugin is not activated', 'spocket'), 146 'solution' => __( 147 'Please install and activate the WooCommerce plugin', 148 'spocket' 149 ), 150 ); 151 } 152 153 if (!version_compare(wc()->version, '2.6', '>=')) { 154 return array( 155 'title' => __('WooCommerce', 'spocket'), 156 'pass' => false, 157 'reason' => __( 158 'WooCommerce plugin is out of date, version >= 2.6 is required', 159 'spocket' 160 ), 161 'solution' => __( 162 'Please update your WooCommerce plugin', 163 'spocket' 164 ), 165 ); 166 } 167 168 return array( 169 'title' => __('WooCommerce', 'spocket'), 170 'pass' => true, 171 ); 172 } 173 174 /** 175 * Checks WordPress requirements. 176 * 177 * @return array WordPress requirements check results 178 */ 179 public static function getWordPressRequirementsStatus() 180 { 181 if (!version_compare(get_bloginfo('version'), '4.4', '>=')) { 182 return array( 183 'title' => __('WordPress', 'spocket'), 184 'pass' => false, 185 'reason' => __( 186 'WordPress is out of date, version >= 4.4 is required', 187 'spocket' 188 ), 189 'solution' => __( 190 'Please update your WordPress installation', 191 'spocket' 192 ), 193 ); 194 } 195 196 return array( 197 'title' => __('WordPress', 'spocket'), 198 'pass' => true, 199 ); 200 } 201 202 /** 203 * Checks permalinks requirements. 204 * 205 * @return array Permalinks requirements check results 206 */ 207 public static function getPermalinksRequirementsStatus() 208 { 209 if (get_option('permalink_structure', '') === '') { 210 return array( 211 'title' => __('Permalinks', 'spocket'), 212 'pass' => false, 213 'reason' => __( 214 'Permalinks set to "Plain"', 215 'spocket' 216 ), 217 'solution' => __( 218 'Set permalinks to anything other than "Plain" '. 219 'in <b>Settings > Permalinks</b>', 220 'spocket' 221 ), 222 ); 223 } 224 225 return array( 226 'title' => __('Permalinks', 'spocket'), 227 'pass' => true, 228 ); 229 } 230 231 /** 232 * Retrieves collective requirements status. 233 * 234 * @return array Requirements check results 235 */ 236 public static function getRequirementsStatus() 237 { 238 $requirementsStatus = array( 239 'woocommerce' => self::getWooCommerceRequirementsStatus(), 240 'wordpress' => self::getWordPressRequirementsStatus(), 241 'permalinks' => self::getPermalinksRequirementsStatus(), 242 'user' => self::getUserRequirementsStatus(), 243 'store_name' => self::getStoreNameRequirementsStatus(), 244 'check_ssl' => self::checkSSLConnection(), 245 'check_curl' => self::checkCurl(), 246 ); 247 return $requirementsStatus; 248 } 249 250 /** 251 * Api url. 252 * 253 * @return string 254 */ 255 private static function _getApiURL() 256 { 257 $plugin = new \Spocket\Plugin(); 258 return $plugin->_getApiURL(); 259 } 220 260 } -
spocket/trunk/lib/Common/Spocket.php
r2409288 r2762014 1 1 <?php 2 2 3 /** 4 * File for Requirements class 5 * php version 7.4 6 * 7 * @category Category 8 * @package Spocket\Common 9 * @author Display name <[email protected]> 10 * @license Copyright https://en.wikipedia.org/wiki/Copyright 11 * @link no link 12 */ 3 13 namespace Spocket\Common; 4 14 5 15 /** 6 16 * Handles Spocket related functionality 17 * 18 * @category Category 19 * @package Spocket\Common 20 * @author Display name <[email protected]> 21 * @license Copyright https://en.wikipedia.org/wiki/Copyright 22 * @link no link 7 23 */ 8 class Spocket { 9 10 /** 11 * The plugin instance. 12 * 13 * @var Plugin 14 */ 15 private $plugin; 16 17 private static $object = null; 18 19 private function getSpocketConsumerSecret() { 20 global $wpdb; 21 22 $wpUser = wp_get_current_user(); 23 $tableName = $wpdb->prefix . 'woocommerce_api_keys'; 24 25 return $wpdb->get_row($wpdb->prepare( 26 'SELECT consumer_secret 27 FROM `%1$s` 28 WHERE description LIKE %s AND user_id = "%3$s"', $tableName, $wpdb->esc_like('%spocket%'), $wpUser->id)); 29 } 30 31 private function getProductCategory( $category_id) { 32 global $wpdb; 33 34 $tableName = $wpdb->prefix . 'terms'; 35 return $wpdb->get_row($wpdb->prepare('select * from `%1$s` where term_id = "%2$s"', $tableName, $category_id)); 36 } 37 38 /** 39 * Sets up initial instance properties 40 * 41 * @param \Spocket\Plugin $plugin This plugin's instance. 42 * @return void 43 */ 44 public function __construct( \Spocket\Plugin $plugin) { 45 self::$object = $this; 46 $this->plugin = $plugin; 47 } 48 49 /** 50 * Checks whether Spocket is integrated with the store. 51 * 52 * @return array Spocket status results 53 */ 54 public static function getSpocketStatus() { 55 return array( 56 'connected' => get_option('spocket_connected', false), 57 ); 58 } 59 60 /** 61 * Filters request ags and adds the data that Spocket expects. 62 * 63 * @param array http args 64 * @param string url 65 * @return array modified http args 66 */ 67 public static function fillInSpocketData( $args, $url) { 68 if (!is_user_logged_in()) { 69 return $args; 70 } 71 72 $urlHost = parse_url($url, PHP_URL_HOST); 73 74 if (!getenv('IS_DEV') && 'spocket.co' !== $urlHost && '.spocket.co' !== substr($urlHost, -11)) { 75 return $args; 76 } 77 78 $body = json_decode($args['body'], true); 79 80 if (!is_array($body)) { 81 return $args; 82 } 83 84 if (!isset($body['consumer_key'])) { 85 return $args; 86 } 87 88 // We are sending API keys to spocket, set our option here 89 $currentUser = wp_get_current_user(); 90 91 $body['user_email'] = $currentUser->get('user_email'); 92 $body['user_name'] = $currentUser->get('display_name'); 93 94 $body['store_authorization_key'] = get_option('spocket_store_authorization_key'); 95 96 $customer = new \WC_Customer($currentUser->ID); 97 98 $body['user_address'] = sprintf( 99 '%s, %s %s, %s', 100 $customer->get_billing_address_1(), 101 $customer->get_billing_postcode(), 102 $customer->get_billing_city(), 103 $customer->get_billing_country() 104 ); 105 106 if (empty(trim(str_replace(',', '', $body['user_address'])))) { 107 $body['user_address'] = ''; 108 } 109 110 $body['store_name'] = get_option('blogname'); 111 $body['store_url'] = get_site_url(); 112 $args['body'] = wp_json_encode($body); 113 114 return apply_filters('spocket_http_args', $args, $url); 115 } 116 117 /** 118 * Intercepts response and stores an auth token. 119 * 120 * @param array http request response 121 * @param array http args 122 * @param string url 123 * @return array untouched http request response 124 */ 125 public static function maybeSaveSpocketAuthToken( $response, $r, $url) { 126 $urlHost = parse_url($url, PHP_URL_HOST); 127 128 if (!getenv('IS_DEV') && 'spocket.co' !== $urlHost && substr($urlHost, -11) !== '.spocket.co') { 129 return $response; 130 } 131 132 if (is_wp_error($response) || intval($response['response']['code']) !== 200) { 133 return $response; 134 } 135 136 $body = json_decode($response['body'], true); 137 138 if (!is_array($body)) { 139 return $response; 140 } 141 142 if (!isset($body['auth_token'])) { 143 return $response; 144 } 145 146 update_option('spocket_auth_token', $body['auth_token'], false); 147 update_option('spocket_connected', true, false); 148 return $response; 149 } 150 151 /** 152 * Disconnects Spocket from the store. 153 * 154 * @return boolean 155 */ 156 public static function disconnectSpocket () { 157 global $wpdb; 158 159 update_option('spocket_connected', false, false); 160 161 $spocketKey = self::$object->getSpocketConsumerSecret(); 162 163 if (empty($spocketKey)) { 164 return true; 165 } 166 167 $consumerSecret = $spocketKey->consumer_secret; 168 $userId = get_option('spocket_user_id', ''); 169 $wpdb->query( 170 " 24 class Spocket 25 { 26 27 /** 28 * The plugin instance. 29 * 30 * @var Plugin 31 */ 32 private $_plugin; 33 34 private static $_object = null; 35 /** 36 * Get Spocket Consumer Secret 37 * 38 * @return void 39 */ 40 private function _getSpocketConsumerSecret() 41 { 42 global $wpdb; 43 44 $wpUser = wp_get_current_user(); 45 $tableName = $wpdb->prefix . 'woocommerce_api_keys'; 46 47 return $wpdb->get_row( 48 $wpdb->prepare( 49 'SELECT consumer_secret 50 FROM `%1$s` 51 WHERE description LIKE %s 52 AND user_id = "%3$s"', 53 $tableName, 54 $wpdb->esc_like('%spocket%'), 55 $wpUser->id 56 ) 57 ); 58 } 59 60 /** 61 * Product Category 62 * 63 * @param $category_id Category Id 64 * 65 * @return array 66 */ 67 private function _getProductCategory( $category_id) 68 { 69 global $wpdb; 70 71 $tableName = $wpdb->prefix . 'terms'; 72 return $wpdb->get_row( 73 $wpdb->prepare( 74 'select * from `%1$s` where term_id = "%2$s"', 75 $tableName, 76 $category_id 77 ) 78 ); 79 } 80 81 /** 82 * Sets up initial instance properties 83 * 84 * @param \Spocket\Plugin $plugin This plugin's instance. 85 * 86 * @return void 87 */ 88 public function __construct( \Spocket\Plugin $plugin) 89 { 90 self::$_object = $this; 91 $this->_plugin = $plugin; 92 } 93 94 /** 95 * Checks whether Spocket is integrated with the store. 96 * 97 * @return array Spocket status results 98 */ 99 public static function getSpocketStatus() 100 { 101 return array( 102 'connected' => get_option('spocket_connected', false), 103 ); 104 } 105 106 /** 107 * Filters request ags and adds the data that Spocket expects. 108 * 109 * @param array $args http args 110 * @param string $url url 111 * 112 * @return array modified http args 113 */ 114 public static function fillInSpocketData( $args, $url) 115 { 116 if (!is_user_logged_in()) { 117 return $args; 118 } 119 120 $urlHost = parse_url($url, PHP_URL_HOST); 121 122 if (!getenv('IS_DEV') 123 && 'spocket.co' !== $urlHost 124 && '.spocket.co' !== substr($urlHost, -11) 125 ) { 126 return $args; 127 } 128 129 $body = json_decode($args['body'], true); 130 131 if (!is_array($body)) { 132 return $args; 133 } 134 135 if (!isset($body['consumer_key'])) { 136 return $args; 137 } 138 139 // We are sending API keys to spocket, set our option here 140 $currentUser = wp_get_current_user(); 141 142 $body['user_email'] = $currentUser->get('user_email'); 143 $body['user_name'] = $currentUser->get('display_name'); 144 145 $body['store_authorization_key'] = get_option( 146 'spocket_store_authorization_key' 147 ); 148 149 $customer = new \WC_Customer($currentUser->ID); 150 151 $body['user_address'] = sprintf( 152 '%s, %s %s, %s', 153 $customer->get_billing_address_1(), 154 $customer->get_billing_postcode(), 155 $customer->get_billing_city(), 156 $customer->get_billing_country() 157 ); 158 159 if (empty(trim(str_replace(',', '', $body['user_address'])))) { 160 $body['user_address'] = ''; 161 } 162 163 $body['store_name'] = get_option('blogname'); 164 $body['store_url'] = get_site_url(); 165 $args['body'] = wp_json_encode($body); 166 167 return apply_filters('spocket_http_args', $args, $url); 168 } 169 170 /** 171 * Intercepts response and stores an auth token. 172 * 173 * @param array $response http request response 174 * @param array $r http args 175 * @param string $url url 176 * 177 * @return array untouched http request response 178 */ 179 public static function maybeSaveSpocketAuthToken( $response, $r, $url) 180 { 181 $urlHost = parse_url($url, PHP_URL_HOST); 182 183 if (!getenv('IS_DEV') 184 && 'spocket.co' !== $urlHost 185 && substr($urlHost, -11) !== '.spocket.co' 186 ) { 187 return $response; 188 } 189 190 if (is_wp_error($response) 191 || intval($response['response']['code']) !== 200 192 ) { 193 return $response; 194 } 195 196 $body = json_decode($response['body'], true); 197 198 if (!is_array($body)) { 199 return $response; 200 } 201 202 if (!isset($body['auth_token'])) { 203 return $response; 204 } 205 206 update_option('spocket_auth_token', $body['auth_token'], false); 207 update_option('spocket_connected', true, false); 208 return $response; 209 } 210 211 /** 212 * Disconnects Spocket from the store. 213 * 214 * @return boolean 215 */ 216 public static function disconnectSpocket() 217 { 218 global $wpdb; 219 220 update_option('spocket_connected', false, false); 221 222 $spocketKey = self::$_object->_getSpocketConsumerSecret(); 223 224 if (empty($spocketKey)) { 225 return true; 226 } 227 228 $consumerSecret = $spocketKey->consumer_secret; 229 $userId = get_option('spocket_user_id', ''); 230 $wpdb->query( 231 " 171 232 DELETE 172 233 FROM {$wpdb->prefix}woocommerce_api_keys 173 234 WHERE description LIKE '%spocket%' 174 235 " 175 ); 176 177 if (empty($userId)) { 178 return true; 179 } 180 181 $deleteIntegrationResponse = wp_remote_request( 182 sprintf( 183 self::$object->plugin->getApiURL() . '/woocommerce/integration?user_id=%s&consumer_secret=%s', 184 $userId, 185 $consumerSecret 186 ), 187 array( 188 'method' => 'DELETE', 189 ) 190 ); 191 192 return true; 193 } 194 195 /** 196 * Saves the user id to the database upon successful Spocket connection. 197 * 198 * @return void 199 */ 200 public function maybeSaveUserId() { 201 if (!isset($_GET['success']) || !isset($_GET['user_id'])) { 202 return; 203 } 204 205 if (intval($_GET['success']) !== 1) { 206 return; 207 } 208 209 $savedUserId = get_option('spocket_user_id', ''); 210 $newUserId = sanitize_text_field($_GET['user_id']); 211 212 if ($savedUserId === $newUserId) { 213 return; 214 } 215 216 update_option('spocket_user_id', $newUserId, false); 217 } 218 219 /** 220 * Saves spocket_shop_url if the option is empty on wp_options table. 221 * 222 * @return void 223 */ 224 public static function saveShopUrl() { 225 $shopUrl = get_option('spocket_shop_url', ''); 226 $site_url = get_site_url(); 227 //if ('' === $shopUrl) { 228 update_option('spocket_shop_url', $site_url, false); 229 //} 230 } 231 232 233 /** 234 * Finishes the Direct Signup process 235 * 236 * @return void 237 */ 238 public static function finishDirectSignup( $storeAuthorizationKey) { 239 /** 240 * Authorization key generated when the user signs up through 241 * direct signup process. 242 * PS. That is not our Spocket Auth Token key. 243 */ 244 if ('' !== $storeAuthorizationKey) { 245 update_option('spocket_store_authorization_key', $storeAuthorizationKey, false); 246 } 247 248 return true; 249 } 250 251 /** 252 * Add the spocket_shop_url to webhook payload. 253 * 254 * @return $payload 255 */ 256 public function addAdditionalParamsToWebhookPayload( $payload) { 257 $shopUrl = get_option('spocket_shop_url', ''); 258 $payload['spocket_shop_url'] = $shopUrl; 259 $payload['spocket_plugin_version'] = $this->plugin->getVersion(); 260 $payload['spocket_integrated_store_id'] = get_option('spocket_user_id', ''); 261 262 return $payload; 263 } 264 265 /** 266 * Just a over write for the Webhook disable rule 267 * to explain that, the webhooks have a limit of 5 failed requests, 268 * after that the webhooks will be disabled on its own. 269 */ 270 public function overruleWebhookDisableRule() { 271 return 999999999999; 272 } 273 274 /** 275 * Action to create a collection on Spocket when it's created on WooCommerce 276 */ 277 public function createCategoryOnSpocket( $category_id) { 278 $category = $this->getProductCategory($category_id); 279 $spocketAuthToken = get_option('spocket_auth_token'); 280 281 $requestBody = array( 282 'collection' => array( 283 'uid' => $category_id, 284 'title' => $category->name, 285 'handle' => $category->slug 286 ) 287 ); 288 289 if (!empty($spocketAuthToken)) { 290 wp_safe_remote_post( 291 $this->plugin->getApiURL() . '/dropshippers/woocommerces/collections', 292 array( 293 'headers' => array( 294 'Content-Type' => 'application/json', 295 'Authorization' => 'Bearer ' . $spocketAuthToken 296 ), 297 'body' => json_encode($requestBody) 298 ) 299 ); 300 } 301 } 302 303 public function deleteCategoryOnSpocket( $category_id) { 304 $category = $this->getProductCategory($category_id); 305 $spocketAuthToken = get_option('spocket_auth_token'); 306 307 if (!empty($spocketAuthToken)) { 308 wp_remote_request( 309 $this->plugin->getApiURL() . '/dropshippers/woocommerces/collections', 310 array( 311 'method' => 'DELETE', 312 'headers' => array( 313 'Content-Type' => 'application/json', 314 'Authorization' => 'Bearer ' . $spocketAuthToken 315 ), 316 'body' => json_encode(array('uid' => $category_id)) 317 ) 318 ); 319 } 320 } 321 322 /** 323 * Registers filters and actions. 324 * 325 * @return void 326 */ 327 public function run() { 328 self::saveShopUrl(); // save spocket_shop_url 329 330 add_filter('http_request_args', array($this, 'fillInSpocketData'), 10, 2); 331 add_filter('http_response', array($this, 'maybeSaveSpocketAuthToken'), 10, 3); 332 add_action('spocket_before_interface', array($this, 'maybeSaveUserId')); 333 334 // change default woocommerce webhook requests 335 add_filter('woocommerce_webhook_payload', array($this, 'addAdditionalParamsToWebhookPayload'), 10, 3); 336 337 // overrule woocommerce disable webhooks rule 338 add_filter( 'woocommerce_max_webhook_delivery_failures', array($this, 'overruleWebhookDisableRule')); 339 340 // create category on Spocket 341 add_action('create_product_cat', array($this, 'createCategoryOnSpocket')); 342 343 // delete category on Spocket 344 add_action('delete_product_cat', array($this, 'deleteCategoryOnSpocket')); 345 } 236 ); 237 238 if (empty($userId)) { 239 return true; 240 } 241 242 $deleteIntegrationResponse = wp_remote_request( 243 sprintf( 244 self::$_object->plugin->getApiURL() . 245 '/woocommerce/integration?user_id=%s&consumer_secret=%s', 246 $userId, 247 $consumerSecret 248 ), 249 array( 250 'method' => 'DELETE', 251 ) 252 ); 253 254 return true; 255 } 256 257 /** 258 * Saves the user id to the database upon successful Spocket connection. 259 * 260 * @return void 261 */ 262 public function maybeSaveUserId() 263 { 264 if (!isset($_GET['success']) || !isset($_GET['user_id'])) { 265 return; 266 } 267 268 if (intval($_GET['success']) !== 1) { 269 return; 270 } 271 272 $savedUserId = get_option('spocket_user_id', ''); 273 $newUserId = sanitize_text_field($_GET['user_id']); 274 275 if ($savedUserId === $newUserId) { 276 return; 277 } 278 279 update_option('spocket_user_id', $newUserId, false); 280 } 281 282 /** 283 * Saves spocket_shop_url if the option is empty on wp_options table. 284 * 285 * @return void 286 */ 287 public static function saveShopUrl() 288 { 289 $shopUrl = get_option('spocket_shop_url', ''); 290 $site_url = get_site_url(); 291 //if ('' === $shopUrl) { 292 update_option('spocket_shop_url', $site_url, false); 293 //} 294 } 295 296 297 /** 298 * Finishes the Direct Signup process 299 * 300 * @param $storeAuthorizationKey Store authorization Key 301 * 302 * @return void 303 */ 304 public static function finishDirectSignup( $storeAuthorizationKey) 305 { 306 /** 307 * Authorization key generated when the user signs up through 308 * direct signup process. 309 * PS. That is not our Spocket Auth Token key. 310 */ 311 if ('' !== $storeAuthorizationKey) { 312 update_option( 313 'spocket_store_authorization_key', 314 $storeAuthorizationKey, 315 false 316 ); 317 } 318 319 return true; 320 } 321 322 /** 323 * Add the spocket_shop_url to webhook payload. 324 * 325 * @param $payload Payload 326 * 327 * @return $payload 328 */ 329 public function addAdditionalParamsToWebhookPayload( $payload) 330 { 331 $shopUrl = get_option('spocket_shop_url', ''); 332 $payload['spocket_shop_url'] = $shopUrl; 333 $payload['spocket_plugin_version'] = $this->_plugin->getVersion(); 334 $payload['spocket_integrated_store_id'] = get_option('spocket_user_id', ''); 335 336 return $payload; 337 } 338 339 /** 340 * Just a over write for the Webhook disable rule 341 * to explain that, the webhooks have a limit of 5 failed requests, 342 * after that the webhooks will be disabled on its own. 343 * 344 * @return integer 345 */ 346 public function overruleWebhookDisableRule() 347 { 348 return 999999999999; 349 } 350 351 /** 352 * Action to create a collection on Spocket when it's created on WooCommerce 353 * 354 * @param $category_id Category Id 355 * 356 * @return void 357 */ 358 public function createCategoryOnSpocket( $category_id) 359 { 360 $category = $this->_getProductCategory($category_id); 361 $spocketAuthToken = get_option('spocket_auth_token'); 362 363 $requestBody = array( 364 'collection' => array( 365 'uid' => $category_id, 366 'title' => $category->name, 367 'handle' => $category->slug 368 ) 369 ); 370 371 if (!empty($spocketAuthToken)) { 372 wp_safe_remote_post( 373 $this->_plugin->getApiURL().'/dropshippers/woocommerces/collections', 374 array( 375 'headers' => array( 376 'Content-Type' => 'application/json', 377 'Authorization' => 'Bearer ' . $spocketAuthToken 378 ), 379 'body' => json_encode($requestBody) 380 ) 381 ); 382 } 383 } 384 385 /** 386 * DeleteCategoryOnSpocket 387 * 388 * @param $category_id Category Id 389 * 390 * @return void 391 */ 392 public function deleteCategoryOnSpocket( $category_id) 393 { 394 $category = $this->_getProductCategory($category_id); 395 $spocketAuthToken = get_option('spocket_auth_token'); 396 397 if (!empty($spocketAuthToken)) { 398 wp_remote_request( 399 $this->_plugin->getApiURL().'/dropshippers/woocommerces/collections', 400 array( 401 'method' => 'DELETE', 402 'headers' => array( 403 'Content-Type' => 'application/json', 404 'Authorization' => 'Bearer ' . $spocketAuthToken 405 ), 406 'body' => json_encode(array('uid' => $category_id)) 407 ) 408 ); 409 } 410 } 411 412 /** 413 * Registers filters and actions. 414 * 415 * @return void 416 */ 417 public function run() 418 { 419 self::saveShopUrl(); // save spocket_shop_url 420 421 add_filter('http_request_args', array($this, 'fillInSpocketData'), 10, 2); 422 add_filter( 423 'http_response', 424 array($this, 'maybeSaveSpocketAuthToken'), 425 10, 426 3 427 ); 428 add_action('spocket_before_interface', array($this, 'maybeSaveUserId')); 429 430 // change default woocommerce webhook requests 431 add_filter( 432 'woocommerce_webhook_payload', 433 array($this, 'addAdditionalParamsToWebhookPayload'), 434 10, 435 3 436 ); 437 438 // overrule woocommerce disable webhooks rule 439 add_filter( 440 'woocommerce_max_webhook_delivery_failures', 441 array($this, 'overruleWebhookDisableRule') 442 ); 443 444 // create category on Spocket 445 add_action('create_product_cat', array($this, 'createCategoryOnSpocket')); 446 447 // delete category on Spocket 448 add_action('delete_product_cat', array($this, 'deleteCategoryOnSpocket')); 449 } 346 450 } -
spocket/trunk/lib/Plugin.php
r2745504 r2762014 1 1 <?php 2 2 3 /** 4 * File for Requirements class 5 * php version 1.6.9 6 * 7 * @category Category 8 * @package Spocket 9 * @author Display name <[email protected]> 10 * @license Copyright https://en.wikipedia.org/wiki/Copyright 11 * @link no link 12 */ 3 13 namespace Spocket; 4 14 5 15 /** 6 16 * The core plugin class 17 * 18 * @category Category 19 * @package Spocket 20 * @author Display name <[email protected]> 21 * @license Copyright https://en.wikipedia.org/wiki/Copyright 22 * @link no link 7 23 */ 8 class Plugin { 9 10 /** 11 * The absolute url to the plugin folder. 12 * 13 * @var string 14 */ 15 protected $url; 16 17 /** 18 * The absolute path to the plugin folder. 19 * 20 * @var string 21 */ 22 protected $path; 23 24 /** 25 * The relative path with the index.php to the plugin folder. 26 * 27 * @var string 28 */ 29 protected $pluginBaseName; 30 31 /** 32 * Plugin version. 33 * 34 * @var string 35 */ 36 protected $version = '1.6.8'; 37 38 /**1000 39 * Sets up initial instance properties 40 * 41 * @return void 42 */ 43 public function __construct() { 44 $this->url = \plugin_dir_url(dirname(__FILE__)); 45 $this->path = \plugin_dir_path(dirname(__FILE__)); 46 $this->pluginBaseName = \plugin_basename(MAKE_PATHS_RELATIVE_FILE); 47 } 48 49 /** 50 * Retrieve the url of the plugin 51 * 52 * @return string 53 */ 54 public function getUrl() { 55 return $this->url; 56 } 57 58 /** 59 * Retrieve the path of the plugin 60 * 61 * @return string 62 */ 63 public function getPath() { 64 return $this->path; 65 } 66 67 /** 68 * Retrieve the plugin_basename 69 * 70 * @return string 71 */ 72 public function getPluginBaseName() { 73 return $this->pluginBaseName; 74 } 75 76 /** 77 * Retrieve the plugin version 78 * 79 * @return string 80 */ 81 public function getVersion() { 82 return $this->version; 83 } 84 85 /** 86 * Retrieve the absolute url for the plugin assets folder 87 * 88 * @return string 89 */ 90 public function getAssetsURL() { 91 if (\getenv('ASSETS_URL') === false) { 92 return $this->getURL() . 'dist/'; 93 } 94 95 return \getenv('ASSETS_URL'); 96 } 97 98 /** 99 * Retrieve the absolute url for the plugin assets src folder 100 * 101 * @return string 102 */ 103 public function getAssetsSrcURL() { 104 return $this->getURL() . 'src/'; 105 } 106 107 /** 108 * Retrieve the absolute url for the plugin assets src folder 109 * 110 * @return string 111 */ 112 public function getApiURL() { 113 return ( \getenv('API_URL') === false ) ? 'https://newapi.spocket.co' : \getenv('API_URL'); 114 } 115 116 117 /** 118 * Runs the initial setup of the plugin, including assets and hooks. 119 * 120 * @return void 121 */ 122 public function run() { 123 if (wp_doing_ajax()) { 124 $AJAX = new \Spocket\AJAX($this); 125 $AJAX->run(); 126 } elseif (is_admin()) { 127 $Backend = new \Spocket\Backend($this); 128 $Backend->run(); 129 } 130 131 $ActivationCheck = new \Spocket\ActivationCheck\Requirements($this); 132 $ActivationCheck->run(); 133 134 if (in_array('woocommerce/woocommerce.php', apply_filters('activate_plugins', get_option('active_plugins')))) { 135 $CurrencyUpdate = new \Spocket\Webhook\CurrencyUpdate(new Webhook\Data()); 136 $CurrencyUpdate->run(); 137 } 138 139 $this->addCurrencyUpdateCustomTopic(); 140 141 $Common = new \Spocket\Common($this); 142 $Common->run(); 143 } 144 145 private function addProductCategoryUpdateCustomTopic() { 146 $name = 'Product Category Update'; 147 $topic = 'action.created_product_cat'; 148 $deliveryEndpoint = 'webhooks/woocommerces/categories/update'; 149 150 $AddCategoryCustomTopic = new \Spocket\Webhook\AddCustomTopic($name, $topic, $deliveryEndpoint); 151 $AddCategoryCustomTopic->run(); 152 } 153 154 private function addCurrencyUpdateCustomTopic() { 155 $name = 'Currency Update'; 156 $topic = 'action.woocommerce_settings_saved'; 157 $deliveryEndpoint = 'webhooks/woocommerces/shop/update'; 158 159 $AddCategoryCustomTopic = new \Spocket\Webhook\AddCustomTopic($name, $topic, $deliveryEndpoint); 160 $AddCategoryCustomTopic->run(); 161 } 24 class Plugin 25 { 26 27 /** 28 * The absolute url to the plugin folder. 29 * 30 * @var string 31 */ 32 protected $url; 33 34 /** 35 * The absolute path to the plugin folder. 36 * 37 * @var string 38 */ 39 protected $path; 40 41 /** 42 * The relative path with the index.php to the plugin folder. 43 * 44 * @var string 45 */ 46 protected $pluginBaseName; 47 48 /** 49 * Plugin version. 50 * 51 * @var string 52 */ 53 protected $version = '1.6.9'; 54 55 /** 56 * 1000 57 * Sets up initial instance properties 58 * 59 * @return void 60 */ 61 public function __construct() 62 { 63 $this->url = \plugin_dir_url(dirname(__FILE__)); 64 $this->path = \plugin_dir_path(dirname(__FILE__)); 65 $this->pluginBaseName = \plugin_basename(MAKE_PATHS_RELATIVE_FILE); 66 } 67 68 /** 69 * Retrieve the url of the plugin 70 * 71 * @return string 72 */ 73 public function getUrl() 74 { 75 return $this->url; 76 } 77 78 /** 79 * Retrieve the path of the plugin 80 * 81 * @return string 82 */ 83 public function getPath() 84 { 85 return $this->path; 86 } 87 88 /** 89 * Retrieve the plugin_basename 90 * 91 * @return string 92 */ 93 public function getPluginBaseName() 94 { 95 return $this->pluginBaseName; 96 } 97 98 /** 99 * Retrieve the plugin version 100 * 101 * @return string 102 */ 103 public function getVersion() 104 { 105 return $this->version; 106 } 107 108 /** 109 * Retrieve the absolute url for the plugin assets folder 110 * 111 * @return string 112 */ 113 public function getAssetsURL() 114 { 115 if (\getenv('ASSETS_URL') === false) { 116 return $this->getURL() . 'dist/'; 117 } 118 119 return \getenv('ASSETS_URL'); 120 } 121 122 /** 123 * Retrieve the absolute url for the plugin assets src folder 124 * 125 * @return string 126 */ 127 public function getAssetsSrcURL() 128 { 129 return $this->getURL() . 'src/'; 130 } 131 132 /** 133 * Retrieve the absolute url for the plugin assets src folder 134 * 135 * @return string 136 */ 137 public function getApiURL() 138 { 139 $lUrl = ( \getenv('API_URL') === false ) ? 140 'https://newapi.spocket.co' : 141 \getenv('API_URL'); 142 143 return $lUrl; 144 } 145 146 147 /** 148 * Runs the initial setup of the plugin, including assets and hooks. 149 * 150 * @return void 151 */ 152 public function run() 153 { 154 if (wp_doing_ajax()) { 155 $AJAX = new \Spocket\AJAX($this); 156 $AJAX->run(); 157 } elseif (is_admin()) { 158 $Backend = new \Spocket\Backend($this); 159 $Backend->run(); 160 } 161 162 $ActivationCheck = new \Spocket\ActivationCheck\Requirements($this); 163 $ActivationCheck->run(); 164 165 $conditionResult = in_array( 166 'woocommerce/woocommerce.php', 167 apply_filters( 168 'activate_plugins', 169 get_option('active_plugins') 170 ) 171 ); 172 173 if ($conditionResult) { 174 $CurrencyUpdate = new \Spocket\Webhook\CurrencyUpdate( 175 new Webhook\Data() 176 ); 177 $CurrencyUpdate->run(); 178 } 179 180 $this->_addCurrencyUpdateCustomTopic(); 181 182 $Common = new \Spocket\Common($this); 183 $Common->run(); 184 } 185 186 /** 187 * AddProductCategoryUpdateCustomTopic 188 * 189 * @return void 190 */ 191 private function _addProductCategoryUpdateCustomTopic() 192 { 193 $name = 'Product Category Update'; 194 $topic = 'action.created_product_cat'; 195 $deliveryEndpoint = 'webhooks/woocommerces/categories/update'; 196 197 $AddCategoryCustomTopic = new \Spocket\Webhook\AddCustomTopic( 198 $name, 199 $topic, 200 $deliveryEndpoint 201 ); 202 $AddCategoryCustomTopic->run(); 203 } 204 205 /** 206 * AddCurrencyUpdateCustomTopic 207 * 208 * @return void 209 */ 210 private function _addCurrencyUpdateCustomTopic() 211 { 212 $name = 'Currency Update'; 213 $topic = 'action.woocommerce_settings_saved'; 214 $deliveryEndpoint = 'webhooks/woocommerces/shop/update'; 215 216 $AddCategoryCustomTopic = new \Spocket\Webhook\AddCustomTopic( 217 $name, 218 $topic, 219 $deliveryEndpoint 220 ); 221 $AddCategoryCustomTopic->run(); 222 } 162 223 } -
spocket/trunk/lib/Webhook/AddCustomTopic.php
r2164115 r2762014 1 1 <?php 2 2 3 /** 4 * File for Requirements class 5 * php version 7.4 6 * 7 * @category Category 8 * @package Spocket\Webhook 9 * @author Display name <[email protected]> 10 * @license Copyright https://en.wikipedia.org/wiki/Copyright 11 * @link no link 12 */ 3 13 namespace Spocket\Webhook; 4 14 5 class AddCustomTopic { 15 /** 16 * AddCustomTopic 17 * 18 * @category Category 19 * @package Spocket\Webhook 20 * @author Display name <[email protected]> 21 * @license Copyright https://en.wikipedia.org/wiki/Copyright 22 * @link no link 23 */ 24 class AddCustomTopic 25 { 6 26 7 private $topic;8 private $name;9 private $deliveryEndpoint;10 private $apiUrl;27 private $_topic; 28 private $_name; 29 private $_deliveryEndpoint; 30 private $_apiUrl; 11 31 12 private function getSecret() { 13 global $wpdb; 32 /** 33 * Get Secret 34 * 35 * @return array 36 */ 37 private function _getSecret() 38 { 39 global $wpdb; 14 40 15 return $wpdb->get_row($wpdb->prepare('select secret from `%1$s` where status = "active" limit 1', $this->table)); 16 } 41 return $wpdb->get_row( 42 $wpdb->prepare( 43 'select secret from `%1$s` where status = "active" limit 1', 44 $this->table 45 ) 46 ); 47 } 17 48 18 private function queryTopic() { 19 global $wpdb; 20 return $wpdb->get_row($wpdb->prepare('select secret from `%1$s` where status = "active" and topic = "%2$s"', $this->table, $this->topic)); 21 } 49 /** 50 * Query Topic 51 * 52 * @return array 53 */ 54 private function _queryTopic() 55 { 56 global $wpdb; 57 return $wpdb->get_row( 58 $wpdb->prepare( 59 'select secret from `%1$s` '. 60 'where status = "active" and topic = "%2$s"', 61 $this->table, 62 $this->_topic 63 ) 64 ); 65 } 22 66 23 public function __construct( $name, $topic, $deliveryEndpoint) { 24 global $wpdb; 67 /** 68 * Sets up initial instance properties 69 * 70 * @param $name Name. 71 * @param $topic Topic. 72 * @param $deliveryEndpoint EndPoint. 73 * 74 * @return void 75 */ 76 public function __construct( $name, $topic, $deliveryEndpoint) 77 { 78 global $wpdb; 79 $lApiUrl = !empty(getenv('API_URL')) ? 80 getenv('API_URL') : 81 'https://newapi.spocket.co/'; 25 82 26 $this->name = $name;27 $this->topic = $topic;28 $this->deliveryEndpoint = $deliveryEndpoint;29 $this->apiUrl = !empty(getenv('API_URL')) ? getenv('API_URL') : 'https://newapi.spocket.co/';30 $this->table = "{$wpdb->prefix}wc_webhooks";31 }83 $this->_name = $name; 84 $this->_topic = $topic; 85 $this->_deliveryEndpoint = $deliveryEndpoint; 86 $this->_apiUrl = $lApiUrl; 87 $this->table = "{$wpdb->prefix}wc_webhooks"; 88 } 32 89 33 public function add() { 34 if (!empty($this->getSecret())) { 35 $secret = $this->getSecret()->secret; 36 $query_topic = $this->queryTopic(); 37 38 if (!empty($secret) && empty($query_topic)) { 39 $webhook = new \WC_Webhook(); 40 $webhook->set_props(array( 41 'secret' => $secret, 42 'status' => 'active', 43 'api_version' => 2, 44 'name' => $this->name, 45 'delivery_url' => $this->apiUrl . '/' . $this->deliveryEndpoint, 46 'topic' => $this->topic, 47 'user_id' => get_current_user_id() 48 )); 90 /** 91 * Add 92 * 93 * @return void 94 */ 95 public function add() 96 { 97 if (!empty($this->_getSecret())) { 98 $secret = $this->_getSecret()->secret; 99 $query_topic = $this->_queryTopic(); 49 100 50 $webhook->save(); 51 } 52 } 53 } 101 if (!empty($secret) && empty($query_topic)) { 102 $webhook = new \WC_Webhook(); 103 $webhook->set_props( 104 array( 105 'secret' => $secret, 106 'status' => 'active', 107 'api_version' => 2, 108 'name' => $this->_name, 109 'delivery_url' => $this->_apiUrl . 110 '/' . $this->_deliveryEndpoint, 111 'topic' => $this->_topic, 112 'user_id' => get_current_user_id() 113 ) 114 ); 54 115 55 public function run() { 56 add_action('admin_init', array($this, 'add')); 57 } 116 $webhook->save(); 117 } 118 } 119 } 120 121 /** 122 * Run 123 * 124 * @return void 125 */ 126 public function run() 127 { 128 add_action('admin_init', array($this, 'add')); 129 } 58 130 } -
spocket/trunk/lib/Webhook/CurrencyUpdate.php
r2164115 r2762014 1 1 <?php 2 2 3 /** 4 * File for Requirements class 5 * php version 7.4 6 * 7 * @category Category 8 * @package Spocket\Webhook 9 * @author Display name <[email protected]> 10 * @license Copyright https://en.wikipedia.org/wiki/Copyright 11 * @link no link 12 */ 3 13 namespace Spocket\Webhook; 4 14 5 class CurrencyUpdate { 15 /** 16 * CurrencyUpdate 17 * 18 * @category Category 19 * @package Spocket\Webhook 20 * @author Display name <[email protected]> 21 * @license Copyright https://en.wikipedia.org/wiki/Copyright 22 * @link no link 23 */ 24 class CurrencyUpdate 25 { 6 26 7 private $webhook;27 private $_webhook; 8 28 9 private static $object = null;29 private static $_object = null; 10 30 11 public function __construct( Data $webhook) { 12 self::$object = $this; 13 $this->webhook = new \WC_Webhook($webhook->find_webhook_by_topic('action.woocommerce_settings_saved')); 14 } 31 /** 32 * Sets up initial instance properties 33 * 34 * @param Data $webhook A Webhook object. 35 * 36 * @return void 37 */ 38 public function __construct( Data $webhook) 39 { 40 self::$_object = $this; 41 $this->_webhook = new \WC_Webhook( 42 $webhook->findWebhookByTopic('action.woocommerce_settings_saved') 43 ); 44 } 15 45 16 /** 17 * AddCurrencyUpdateTopicHook will add a new webhook topic hook. 18 * 19 * @param array $topic_hooks Esxisting topic hooks. 20 */ 21 public static function addCurrencyUpdateTopicHook( $topic_hooks) { 22 $new_hooks = array( 23 'action.woocommerce_settings_saved' => array( 24 'woocommerce_currency_updated', 25 ), 26 ); 46 /** 47 * AddCurrencyUpdateTopicHook will add a new webhook topic hook. 48 * 49 * @param array $topic_hooks Esxisting topic hooks. 50 * 51 * @return array 52 */ 53 public static function addCurrencyUpdateTopicHook( $topic_hooks) 54 { 55 $new_hooks = array( 56 'action.woocommerce_settings_saved' => array( 57 'woocommerce_currency_updated', 58 ), 59 ); 27 60 28 return array_merge($topic_hooks, $new_hooks);29 }61 return array_merge($topic_hooks, $new_hooks); 62 } 30 63 31 public static function currencyUpdateEvent( $topic_events) { 32 $new_events = array( 33 'woocommerce_settings_saved', 34 ); 64 /** 65 * CurrencyUpdateEvent 66 * 67 * @param array $topic_events topic events. 68 * 69 * @return array 70 */ 71 public static function currencyUpdateEvent( $topic_events) 72 { 73 $new_events = array( 74 'woocommerce_settings_saved', 75 ); 35 76 36 return array_merge($topic_events, $new_events);37 }77 return array_merge($topic_events, $new_events); 78 } 38 79 39 80 40 public static function addCurrencyUpdateTopicToMenu( $topics) { 41 $new_topics = array( 42 'action.woocommerce_settings_saved' => __( 'Currency Update', 'woocommerce' ), 43 ); 81 /** 82 * AddCurrencyUpdateTopicToMenu 83 * 84 * @param array $topics topics. 85 * 86 * @return array 87 */ 88 public static function addCurrencyUpdateTopicToMenu( $topics) 89 { 90 $new_topics = array( 91 'action.woocommerce_settings_saved' => __('Currency Update', 'woocommerce'), 92 ); 44 93 45 return array_merge( $topics, $new_topics);46 }94 return array_merge($topics, $new_topics); 95 } 47 96 48 public static function addCurrencyToWebhookPayload( $payload) { 49 if (array_key_exists('action', $payload) && 'woocommerce_settings_saved' == $payload['action']) { 50 $payload['id'] = self::$object->webhook->get_new_delivery_id(); 51 $payload['currency'] = get_woocommerce_currency(); 52 } 97 /** 98 * AddCurrencyToWebhookPayload 99 * 100 * @param array $payload payload. 101 * 102 * @return object 103 */ 104 public static function addCurrencyToWebhookPayload( $payload) 105 { 106 if (array_key_exists('action', $payload) 107 && 'woocommerce_settings_saved' == $payload['action'] 108 ) { 109 $payload['id'] = self::$_object->webhook->get_new_delivery_id(); 110 $payload['currency'] = get_woocommerce_currency(); 111 } 53 112 54 return $payload;55 }113 return $payload; 114 } 56 115 57 public function run() { 58 add_filter('woocommerce_webhook_topic_hooks', array($this, 'addCurrencyUpdateTopicHook'), 10, 1); 59 add_filter('woocommerce_valid_webhook_events', array($this, 'currencyUpdateEvent'), 10, 1); 60 add_filter('woocommerce_webhook_topics', array($this, 'addCurrencyUpdateTopicToMenu'), 10, 1); 61 add_filter('woocommerce_webhook_payload', array($this, 'addCurrencyToWebhookPayload'), 10, 2); 62 } 116 /** 117 * Run 118 * 119 * @return void 120 */ 121 public function run() 122 { 123 add_filter( 124 'woocommerce_webhook_topic_hooks', 125 array($this, 'addCurrencyUpdateTopicHook'), 126 10, 127 1 128 ); 129 add_filter( 130 'woocommerce_valid_webhook_events', 131 array($this, 'currencyUpdateEvent'), 132 10, 133 1 134 ); 135 add_filter( 136 'woocommerce_webhook_topics', 137 array($this, 'addCurrencyUpdateTopicToMenu'), 138 10, 139 1 140 ); 141 add_filter( 142 'woocommerce_webhook_payload', 143 array($this, 'addCurrencyToWebhookPayload'), 144 10, 145 2 146 ); 147 } 63 148 } -
spocket/trunk/lib/Webhook/Data.php
r2164115 r2762014 1 1 <?php 2 2 3 /** 4 * File for Requirements class 5 * php version 7.4 6 * 7 * @category Category 8 * @package Spocket\Webhook 9 * @author Display name <[email protected]> 10 * @license Copyright https://en.wikipedia.org/wiki/Copyright 11 * @link no link 12 */ 3 13 namespace Spocket\Webhook; 4 14 5 class Data { 15 /** 16 * Data 17 * 18 * @category Category 19 * @package Spocket\Webhook 20 * @author Display name <[email protected]> 21 * @license Copyright https://en.wikipedia.org/wiki/Copyright 22 * @link no link 23 */ 24 class Data 25 { 6 26 7 private $table = 'wc_webhooks';27 private $_table = 'wc_webhooks'; 8 28 9 public function find_webhook_by_topic( $topic) { 10 global $wpdb; 29 /** 30 * Return Webook 31 * 32 * @param $topic The Webhook Topic 33 * 34 * @return void 35 */ 36 public function findWebhookByTopic( $topic) 37 { 38 global $wpdb; 11 39 12 $tableName = $wpdb->prefix . $this->table;40 $tableName = $wpdb->prefix . $this->_table; 13 41 14 return $wpdb->get_row($wpdb->prepare('select * from `%1$s` where topic = "%2$s"', $tableName, $topic)); 15 } 42 return $wpdb->get_row( 43 $wpdb->prepare( 44 'select * from `%1$s` where topic = "%2$s"', 45 $tableName, 46 $topic 47 ) 48 ); 49 } 16 50 } -
spocket/trunk/readme.txt
r2745504 r2762014 3 3 Requires at least: 4.4 4 4 Tested up to: 6.0 5 Stable tag: 1.6. 85 Stable tag: 1.6.9 6 6 7 7 Find fast shipping products from reliable suppliers, import them to [your WooCommerce store](https://www.spocket.co/for-woocommerce) and manage your orders automatically: all for free.
Note: See TracChangeset
for help on using the changeset viewer.