Plugin Directory

Changeset 1743414


Ignore:
Timestamp:
10/09/2017 03:19:43 PM (8 years ago)
Author:
snappic
Message:

Reverts some comsmetic changes that might have introduced a bug, performs some cosmetic and readability cleanups.

Location:
snappic/trunk
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • snappic/trunk/includes/class-snappic-helper.php

    r1734473 r1743414  
    11<?php
    22
    3 if ( ! defined( 'ABSPATH' ) ) exit;
     3if (!defined('ABSPATH')) exit;
    44
    55class Snappic_Helper {
    6 
    76    const TESTPIXEL = '123123123';
    87    const API_HOST_DEFAULT = 'https://api.snappic.io';
     
    2120    */
    2221    public static function get_instance() {
    23         if ( is_null( self::$_instance ) )
     22        if (is_null(self::$_instance))
    2423            self::$_instance = new self();
    2524        return self::$_instance;
    2625    }
    2726
    28 
    2927    /**
    3028    * Fetch the pixel from API
     
    3230    * @since  1.0.0
    3331    */
    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 minutes
     32    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);
    4139        }
    4240
    4341        // 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    }
    6257
    6358    /**
     
    6762    */
    6863    public function get_stored_pixel_id() {
    69         if( ! $this->stored_pixel_id ) {
     64        if (!$this->stored_pixel_id) {
    7065            $settings = Snappic_Integration::instance();
    71             $this->stored_pixel_id = $settings->get_option( 'pixel_id' );
     66            $this->stored_pixel_id = $settings->get_option('pixel_id');
    7267        }
    7368        return $this->stored_pixel_id;
     
    8075    */
    8176    public function get_pixel_id() {
    82         if( ! $this->pixel_id ) {
     77        if (!$this->pixel_id) {
    8378            $this->pixel_id = $this->is_sandboxed() ? self::TESTPIXEL : $this->get_stored_pixel_id();
    8479        }
     
    9186    * @since  1.0.0
    9287    */
    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    }
    9891
    9992    /**
     
    10497    * @since  1.0.0
    10598    */
    106     public function update_options( $options = array() ) {
     99    public function update_options($options = array()) {
    107100        $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    }
    115106
    116107    /**
     
    119110    */
    120111    public function needs_pixel() {
    121 
    122112        $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)))) {
    125114            return true;
    126115        } else {
    127116            return false;
    128117        }
    129 
    130     }
    131 
     118    }
    132119
    133120    /**
     
    136123    */
    137124    public function has_pixel() {
    138         return (bool) ! $this->needs_pixel();
     125        return (bool)!$this->needs_pixel();
    139126    }
    140127
     
    145132     */
    146133    public function is_live() {
    147         return (bool) ! $this->is_sandboxed();
    148     }
    149 
     134        return (bool)!$this->is_sandboxed();
     135    }
    150136
    151137    /**
     
    155141     */
    156142    public function is_sandboxed() {
    157         return (bool) ( 'sandbox' == $this->get_mode() );
     143        return (bool) ('sandbox' == $this->get_mode());
    158144    }
    159145
     
    165151    public function get_mode() {
    166152        $settings = Snappic_Integration::instance();
    167         return $settings->get_option( 'mode' );
     153        return $settings->get_option('mode');
    168154    }
    169155
     
    174160     * @return string
    175161     */
    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()) {
    178164            return self::API_SANDBOX_HOST_DEFAULT;
    179165        }
    180         return $this->getEnvOrDefault( 'SNAPPIC_API_HOST', self::API_HOST_DEFAULT );
     166        return $this->getEnvOrDefault('SNAPPIC_API_HOST', self::API_HOST_DEFAULT);
    181167    }
    182168
     
    187173     * @return string
    188174     */
    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');
    191177    }
    192178
     
    198184     * @return string
    199185     */
    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        );
    202194    }
    203195
     
    206198     */
    207199    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);
    209201    }
    210202
     
    215207     * @return string
    216208     */
    217     public function get_signup_url( $plan = '' ) {
    218 
     209    public function get_signup_url($plan = '') {
    219210        $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');
    224213        $query_args = array(
    225214            'login' => true,
     
    227216            'domain' => urlencode($this->get_site_domain()),
    228217            '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    }
    239236
    240237    /**
     
    243240     * @return string
    244241     */
    245     public function get_site_domain(){
     242    public function get_site_domain () {
    246243        $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    }
    251247
    252248    /**
     
    257253     * @return string
    258254     */
    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;
    262258    }
    263259
  • snappic/trunk/languages/snappic-for-woocommerce.pot

    r1734473 r1743414  
    33msgid ""
    44msgstr ""
    5 "Project-Id-Version: Snappic for WooCommerce 1.0.1\n"
     5"Project-Id-Version: Snappic for WooCommerce 1.0.3\n"
    66"Report-Msgid-Bugs-To: "
    77"https://wordpress.org/support/plugin/snappic-for-woocommerce\n"
  • snappic/trunk/readme.txt

    r1734473 r1743414  
    55Requires at least: 4.7
    66Tested up to: 4.8
    7 Stable tag: 1.0.1
     7Stable tag: 1.0.3
    88License: GPLv3
    99License URI: http://www.gnu.org/licenses/gpl-3.0.html
  • snappic/trunk/snappic-for-woocommerce.php

    r1734473 r1743414  
    1515
    1616class Snappic_Base {
    17     const VERSION = '1.0.1';
     17    const VERSION = '1.0.3';
    1818    const REQUIRED_WOO = '3.1.0';
    1919
Note: See TracChangeset for help on using the changeset viewer.