Changeset 1743414
- Timestamp:
- 10/09/2017 03:19:43 PM (8 years ago)
- Location:
- snappic/trunk
- Files:
-
- 4 edited
-
includes/class-snappic-helper.php (modified) (20 diffs)
-
languages/snappic-for-woocommerce.pot (modified) (1 diff)
-
readme.txt (modified) (1 diff)
-
snappic-for-woocommerce.php (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
-
snappic/trunk/includes/class-snappic-helper.php
r1734473 r1743414 1 1 <?php 2 2 3 if ( ! defined( 'ABSPATH' )) exit;3 if (!defined('ABSPATH')) exit; 4 4 5 5 class Snappic_Helper { 6 7 6 const TESTPIXEL = '123123123'; 8 7 const API_HOST_DEFAULT = 'https://api.snappic.io'; … … 21 20 */ 22 21 public static function get_instance() { 23 if ( is_null( self::$_instance ))22 if (is_null(self::$_instance)) 24 23 self::$_instance = new self(); 25 24 return self::$_instance; 26 25 } 27 26 28 29 27 /** 30 28 * Fetch the pixel from API … … 32 30 * @since 1.0.0 33 31 */ 34 public function fetch_pixel (){35 36 $result = wp_cache_get( 'snappic_pixel_request', 'api_calls');37 38 if ( false === $result) {39 $result = wp_safe_remote_get( $this->get_api_url());40 wp_cache_set( 'snappic_pixel_request', $result, 'api_calls', 900 ); // cache for 15 minutes32 public function fetch_pixel () { 33 34 $result = wp_cache_get('snappic_pixel_request', 'api_calls'); 35 36 if (false === $result) { 37 $result = wp_safe_remote_get($this->get_api_url()); 38 wp_cache_set('snappic_pixel_request', $result, 'api_calls', 900); 41 39 } 42 40 43 41 // Do something with $result 44 if( is_wp_error( $result ) ) { 45 return false; // Bail early 46 } 47 48 $body = wp_remote_retrieve_body( $result ); 49 $data = json_decode( $body ); 50 51 // If Facebook pixel is not empty string, we need to save it. 52 if( isset( $data->facebook_pixel_id ) && '' !== $data->facebook_pixel_id ) { 53 // If the pixel was saved, delete the cached API call 54 if( $this->save_pixel_id( $data->facebook_pixel_id ) ) { 55 wp_cache_delete( 'snappic_pixel_request', 'api_calls' ); 56 } 57 } 58 59 60 } 61 42 if (is_wp_error($result)) { 43 return false; 44 } 45 46 $body = wp_remote_retrieve_body($result); 47 $data = json_decode($body); 48 // No pixel... 49 if (!isset($data->facebook_pixel_id) || $data->facebook_pixel_id == '') { 50 return false; 51 } 52 // If pixel is valid and safe, clear the cache pixel result. 53 if ($this->save_pixel_id($data->facebook_pixel_id)) { 54 wp_cache_delete('snappic_pixel_request', 'api_calls'); 55 } 56 } 62 57 63 58 /** … … 67 62 */ 68 63 public function get_stored_pixel_id() { 69 if ( ! $this->stored_pixel_id) {64 if (!$this->stored_pixel_id) { 70 65 $settings = Snappic_Integration::instance(); 71 $this->stored_pixel_id = $settings->get_option( 'pixel_id');66 $this->stored_pixel_id = $settings->get_option('pixel_id'); 72 67 } 73 68 return $this->stored_pixel_id; … … 80 75 */ 81 76 public function get_pixel_id() { 82 if ( ! $this->pixel_id) {77 if (!$this->pixel_id) { 83 78 $this->pixel_id = $this->is_sandboxed() ? self::TESTPIXEL : $this->get_stored_pixel_id(); 84 79 } … … 91 86 * @since 1.0.0 92 87 */ 93 public function save_pixel_id( $pixel_id ) { 94 $new_options = array( 'pixel_id' => $pixel_id ); 95 return $this->update_options( $new_options ); 96 } 97 88 public function save_pixel_id($pixel_id) { 89 return $this->update_options(array('pixel_id' => $pixel_id)); 90 } 98 91 99 92 /** … … 104 97 * @since 1.0.0 105 98 */ 106 public function update_options( $options = array()) {99 public function update_options($options = array()) { 107 100 $settings = Snappic_Integration::instance(); 108 $old_options = (array) get_option( $settings->get_option_key() ); 109 $new_options = apply_filters( 'woocommerce_settings_api_sanitized_fields_' . $settings->id, $options ); 110 $updated_options = array_merge( $old_options, $new_options ); 111 112 return update_option( $settings->get_option_key(), $updated_options ); 113 } 114 101 $old_options = (array) get_option($settings->get_option_key()); 102 $new_options = apply_filters('woocommerce_settings_api_sanitized_fields_' . $settings->id, $options); 103 $updated_options = array_merge($old_options, $new_options); 104 return update_option($settings->get_option_key(), $updated_options); 105 } 115 106 116 107 /** … … 119 110 */ 120 111 public function needs_pixel() { 121 122 112 $pixel_id = $this->get_stored_pixel_id(); 123 124 if( ( $this->is_sandboxed() && '' == $pixel_id ) || ( $this->is_live() && in_array( $pixel_id, array( '', self::TESTPIXEL ) ) ) ) { 113 if (($this->is_sandboxed() && '' == $pixel_id) || ($this->is_live() && in_array($pixel_id, array('', self::TESTPIXEL)))) { 125 114 return true; 126 115 } else { 127 116 return false; 128 117 } 129 130 } 131 118 } 132 119 133 120 /** … … 136 123 */ 137 124 public function has_pixel() { 138 return (bool) !$this->needs_pixel();125 return (bool)!$this->needs_pixel(); 139 126 } 140 127 … … 145 132 */ 146 133 public function is_live() { 147 return (bool) ! $this->is_sandboxed(); 148 } 149 134 return (bool)!$this->is_sandboxed(); 135 } 150 136 151 137 /** … … 155 141 */ 156 142 public function is_sandboxed() { 157 return (bool) ( 'sandbox' == $this->get_mode());143 return (bool) ('sandbox' == $this->get_mode()); 158 144 } 159 145 … … 165 151 public function get_mode() { 166 152 $settings = Snappic_Integration::instance(); 167 return $settings->get_option( 'mode');153 return $settings->get_option('mode'); 168 154 } 169 155 … … 174 160 * @return string 175 161 */ 176 public function get_api_host( $bypassSandbox = false) {177 if ( ! $bypassSandbox && $this->is_sandboxed()) {162 public function get_api_host($bypassSandbox = false) { 163 if (!$bypassSandbox && $this->is_sandboxed()) { 178 164 return self::API_SANDBOX_HOST_DEFAULT; 179 165 } 180 return $this->getEnvOrDefault( 'SNAPPIC_API_HOST', self::API_HOST_DEFAULT);166 return $this->getEnvOrDefault('SNAPPIC_API_HOST', self::API_HOST_DEFAULT); 181 167 } 182 168 … … 187 173 * @return string 188 174 */ 189 public function get_api_url( $bypassSandbox = false) {190 return add_query_arg( 'domain', $this->get_site_domain(), $this->get_api_host( $bypassSandbox ) . '/stores/current');175 public function get_api_url($bypassSandbox = false) { 176 return add_query_arg('domain', $this->get_site_domain(), $this->get_api_host($bypassSandbox) . '/stores/current'); 191 177 } 192 178 … … 198 184 * @return string 199 185 */ 200 public function get_checkout_tracker_url( $order_id, $bypassSandbox = false ) { 201 return add_query_arg( array( 'store_domain' => $this->get_site_domain(), 'order_id' => $order_id ), $this->get_api_host( $bypassSandbox ) . '/checkout_trackers/record' ); 186 public function get_checkout_tracker_url($order_id, $bypassSandbox = false) { 187 return add_query_arg( 188 array( 189 'store_domain' => $this->get_site_domain(), 190 'order_id' => $order_id 191 ), 192 $this->get_api_host($bypassSandbox).'/checkout_trackers/record' 193 ); 202 194 } 203 195 … … 206 198 */ 207 199 public function get_snappic_admin_url() { 208 return $this->getEnvOrDefault( 'SNAPPIC_ADMIN_URL', self::SNAPPIC_ADMIN_URL_DEFAULT);200 return $this->getEnvOrDefault('SNAPPIC_ADMIN_URL', self::SNAPPIC_ADMIN_URL_DEFAULT); 209 201 } 210 202 … … 215 207 * @return string 216 208 */ 217 public function get_signup_url( $plan = '' ) { 218 209 public function get_signup_url($plan = '') { 219 210 $settings = Snappic_Integration::instance(); 220 221 $consumerKey = $settings->get_option( 'cust_key'); 222 $consumerSecret = $settings->get_option( 'cust_secret' ); 223 211 $consumerKey = $settings->get_option('cust_key'); 212 $consumerSecret = $settings->get_option('cust_secret'); 224 213 $query_args = array( 225 214 'login' => true, … … 227 216 'domain' => urlencode($this->get_site_domain()), 228 217 'access_token' => urlencode($consumerKey.':'.$consumerSecret) 229 ); 230 231 // Validate the plan is either starter or growth. 232 $plan = in_array( $plan, array( 'starter', 'growth' ) ) ? $plan : ''; 233 234 if ($plan) { $query_args['sra_plan'] = $plan; } 235 236 return add_query_arg( $query_args, $this->get_snappic_admin_url() ); 237 } 238 218 ); 219 220 $plan = in_array($plan, array('starter', 'growth')) ? $plan : ''; 221 if ($plan) { 222 $query_args['sra_plan'] = $plan; 223 } 224 225 return add_query_arg($query_args, $this->get_snappic_admin_url()); 226 } 227 228 /** 229 * Return the checkout tracker url for the Snappic API 230 * 231 * @return string 232 */ 233 public function get_login_url() { 234 return add_query_arg('login', '', $this->get_snappic_admin_url()); 235 } 239 236 240 237 /** … … 243 240 * @return string 244 241 */ 245 public function get_site_domain (){242 public function get_site_domain () { 246 243 $domain = get_site_url(); 247 $components = parse_url( $domain ); 248 return strtolower( $components['host'] ); 249 } 250 244 $components = parse_url($domain); 245 return strtolower($components['host']); 246 } 251 247 252 248 /** … … 257 253 * @return string 258 254 */ 259 public function getEnvOrDefault( $key, $default = null) {260 $val = getenv( $key);261 return empty( $val) ? $default : $val;255 public function getEnvOrDefault($key, $default = null) { 256 $val = getenv($key); 257 return empty($val) ? $default : $val; 262 258 } 263 259 -
snappic/trunk/languages/snappic-for-woocommerce.pot
r1734473 r1743414 3 3 msgid "" 4 4 msgstr "" 5 "Project-Id-Version: Snappic for WooCommerce 1.0. 1\n"5 "Project-Id-Version: Snappic for WooCommerce 1.0.3\n" 6 6 "Report-Msgid-Bugs-To: " 7 7 "https://wordpress.org/support/plugin/snappic-for-woocommerce\n" -
snappic/trunk/readme.txt
r1734473 r1743414 5 5 Requires at least: 4.7 6 6 Tested up to: 4.8 7 Stable tag: 1.0. 17 Stable tag: 1.0.3 8 8 License: GPLv3 9 9 License URI: http://www.gnu.org/licenses/gpl-3.0.html -
snappic/trunk/snappic-for-woocommerce.php
r1734473 r1743414 15 15 16 16 class Snappic_Base { 17 const VERSION = '1.0. 1';17 const VERSION = '1.0.3'; 18 18 const REQUIRED_WOO = '3.1.0'; 19 19
Note: See TracChangeset
for help on using the changeset viewer.