Plugin Directory

Changeset 2988403


Ignore:
Timestamp:
11/03/2023 10:03:17 AM (2 years ago)
Author:
relevanz
Message:

support for additional html (consent tools), refactoring / performance improvements, improved menu structure

Location:
releva-nz/trunk
Files:
3 deleted
7 edited

Legend:

Unmodified
Added
Removed
  • releva-nz/trunk/README.txt

    r2747436 r2988403  
    33Tags: e-commerce, sales, advertising, marketing, retargeting, remarketing
    44Requires at least: 3.0.1
    5 Tested up to: 6.0.0
     5Tested up to: 6.4.0
    66Requires PHP: 5.2
    77Stable tag: trunk
     
    7070= 2.1.3 =
    7171* add pagination for large product exports for better performance
     72
     73= 2.1.4 =
     74* improved tracking for custom success pages
  • releva-nz/trunk/admin/class-relevatracking-admin.php

    r2369532 r2988403  
    107107    {
    108108        foreach ($this->options as $option) {
    109             register_setting($this->get_id() . '_group', $this->get_id() . '_' . $option['name'], array($this, 'api_opt_validate'));
    110         }
    111     }
     109            if($option['name'] === 'api_key') {
     110                register_setting($this->get_id() . '_group', $this->get_id() . '_' . $option['name'], array($this, 'api_opt_validate'));
     111            } else {
     112                register_setting($this->get_id() . '_group', $this->get_id() . '_' . $option['name'], array($this, 'store_setting'));
     113            }
     114        }
     115    }
     116
     117    public function store_setting($input)
     118    {
     119        return $input;
     120    }   
    112121
    113122    public function api_opt_validate($input)
    114123    {
     124
    115125        $api_key_post = sanitize_text_field($this->butler_post('relevatracking_api_key'));
    116126
     
    169179        $option['value'] = get_option($option['id']);
    170180        $this->options[$option['id']] = $option;
     181
     182        $option2 = array();
     183        $option2['name'] = 'additional_html';
     184        $option2['id'] = $this->get_id() . '_' . $option2['name'];
     185        $option2['type'] = 'textarea';
     186        $option2['label'] = __('Additional HTML', $this->plugin_name);
     187        $option2['hint'] = __('', $this->plugin_name);
     188        $option2['value'] = get_option($option2['id']);
     189        $this->options[$option2['id']] = $option2;       
    171190    }
    172191
     
    176195        // Add your own main menu item
    177196        // Releva tracking
    178         add_menu_page(
     197         add_menu_page(
    179198            __('releva.nz', $this->plugin_name),
    180199            __('releva.nz', $this->plugin_name),
    181200            'manage_options',
    182201            $this->get_id() . '_menu',
    183             array($this, 'render_admin_menu'),
     202            array($this, 'render_admin_chart'),
    184203            'dashicons-screenoptions',
    185204            self::MENU_POSITION
    186205        );
    187206
    188         add_submenu_page($this->get_id() . '_menu', __('Settings', $this->plugin_name), __('Settings', $this->plugin_name), 'manage_options', $this->get_id() . '_menu');
    189 
    190207        add_submenu_page(
    191208            $this->get_id() . '_menu',
    192             __('releva.nz', $this->plugin_name),
    193             __('releva.nz', $this->plugin_name),
     209            __('Settings', $this->plugin_name),
     210            __('Settings', $this->plugin_name),
    194211            'manage_options',
    195             $this->get_id() . '_chart',
    196             array($this, 'render_admin_chart')
     212            $this->get_id() . '_settings',
     213            array($this, 'render_admin_menu')
    197214        );
    198215    }
  • releva-nz/trunk/admin/partials/admin-menu.php

    r2369532 r2988403  
    2020        _e( 'Settings', $this->plugin_name) ?></h3>
    2121    </p>
    22 
    23     <p style="display:none"><strong><?php _e( 'Link to export products', $this->plugin_name ); ?></strong>: <span style="color:#005ebb"><?php echo home_url() ; ?><strong>/?releva_action=jsonexport</strong></span></p><hr />
    2422
    2523    <?php if ( $this->options ): ?>
  • releva-nz/trunk/includes/class-relevatracking.php

    r2747436 r2988403  
    7070
    7171        $this->plugin_name = 'relevatracking';
    72         $this->version = '2.1.3';
     72        $this->version = '2.1.5';
    7373
    7474        $this->load_dependencies();
  • releva-nz/trunk/public/class-relevatracking-public.php

    r2747436 r2988403  
    2121 * @author     Relevanz <tec@releva.nz>
    2222 */
    23 class Relevatracking_Public {
     23class Relevatracking_Public
     24{
    2425
    2526    /**
     
    4849     * @param      string    $version    The version of this plugin.
    4950     */
    50     public function __construct( $plugin_name, $version ) {
     51    public function __construct($plugin_name, $version)
     52    {
    5153
    5254        $this->plugin_name = $plugin_name;
    5355        $this->version = $version;
     56
    5457        // Is there a client_id ?
    5558        //$this->load_client_id();
    5659        $this->client_id = self::load_client_id();
    5760
    58         add_action( 'releva_csvexport', array( $this, 'csvexport' ) );
    59         add_action( 'releva_callback', array( $this, 'callback' ) );
    60 
    61     }
    62 
    63     //const RELEVATRC_KEY_URL = 'http://api.hyj.mobi/user/get';
     61        add_action('releva_csvexport', array($this, 'csvexport'));
     62        add_action('releva_callback', array($this, 'callback'));
     63
     64
     65        add_action('woocommerce_before_thankyou', array($this, 'retargeting_confirmation'), 40);
     66    }
     67
    6468    protected $apikey;
    6569    protected static $cache = array();
    6670
    67     public static function load_client_id() {
    68 
    69         /*
    70         $this->client_id = (string)get_option( $this->plugin_name . '_client_id' );
    71         if(empty($this->client_id)) {
    72 
    73         }
    74         */
    75 
    76         $apikey = (string)get_option( 'relevatracking_api_key' );
     71    public static function load_client_id()
     72    {
     73        $apikey = (string)get_option('relevatracking_api_key');
    7774        $storeId = md5(__METHOD__ . "::client_id::" . $apikey);
    7875
    79 
    80         if (!isset(self::$cache[$storeId])){
    81 
    82             if($apikey) {
    83 
    84             $queryParams['apikey'] = $apikey;
    85             $url='https://backend.releva.nz/v1/campaigns/get';
    86             $connectUrl = $url. '?' . http_build_query($queryParams);
    87             $data = self::getUrl($connectUrl, 5);
    88            
    89              $response=self::arrayGetValue($data, 'response');
    90              $response = json_decode ($response) ;
    91 
    92             if(!empty($response) && is_object($response) && isset($response->user_id)) {
    93                self::$cache[$storeId] = $response->user_id;
    94             }
    95 
    96             }
    97 
    98         }
    99 
    100         if (isset(self::$cache[$storeId])){
    101             return self::$cache[$storeId] ;
     76        if (!isset(self::$cache[$storeId])) {
     77
     78            if ($apikey) {
     79                $queryParams['apikey'] = $apikey;
     80                $url = 'https://backend.releva.nz/v1/campaigns/get';
     81                $connectUrl = $url . '?' . http_build_query($queryParams);
     82                $data = self::getUrl($connectUrl, 5);
     83
     84                $response = self::arrayGetValue($data, 'response');
     85                $response = json_decode($response);
     86
     87                if (!empty($response) && is_object($response) && isset($response->user_id)) {
     88                    self::$cache[$storeId] = $response->user_id;
     89                }
     90            }
     91        }
     92
     93        if (isset(self::$cache[$storeId])) {
     94            return self::$cache[$storeId];
    10295        } else {
    10396            return 0;
    10497        }
    105 
    106     }
    107 
    108 
    109 
    110     public function releva_init_action(){
    111         //releva_action=jsonexport
    112         $releva_action = isset($_GET['releva_action'])?$_GET['releva_action']:'';
    113         //echo "<pre>"; var_export($releva_action); echo "</pre>";
    114         // exit;
    115         if($releva_action) {
    116         $releva_action = preg_replace('/\W/', '', $releva_action);
    117         do_action('releva_'.$releva_action);
    118         }
    119        }
    120 
    121     public function getPhpVersion() {
     98    }
     99
     100    public function releva_init_action()
     101    {
     102        $releva_action = isset($_GET['releva_action']) ? $_GET['releva_action'] : '';
     103        if ($releva_action) {
     104            $releva_action = preg_replace('/\W/', '', $releva_action);
     105            do_action('releva_' . $releva_action);
     106        }
     107    }
     108
     109    public function getPhpVersion()
     110    {
    122111        return [
    123112            'version' => phpversion(),
    124113            'sapi-name' => php_sapi_name(),
    125             'memory-limit' => ini_get( 'memory_limit' ),
    126             'max-execution-time' => ini_get( 'max_execution_time' ),
     114            'memory-limit' => ini_get('memory_limit'),
     115            'max-execution-time' => ini_get('max_execution_time'),
    127116        ];
    128117    }
    129118
    130     public function getDbVersion() {
     119    public function getDbVersion()
     120    {
    131121        global $wpdb;
    132122
    133         $version_comment = $wpdb->get_var( 'SELECT @@version_comment AS `server`' );
     123        $version_comment = $wpdb->get_var('SELECT @@version_comment AS `server`');
    134124        return [
    135             //'version' => $wpdb->db_server_info(),
    136125            'server' => $version_comment,
    137126        ];
    138127    }
    139128
    140     public function getServerEnvironment() {
     129    public function getServerEnvironment()
     130    {
    141131        return [
    142             'server-software' => isset( $_SERVER['SERVER_SOFTWARE'] ) ? $_SERVER['SERVER_SOFTWARE'] : null,
     132            'server-software' => isset($_SERVER['SERVER_SOFTWARE']) ? $_SERVER['SERVER_SOFTWARE'] : null,
    143133            'php' => $this->getPhpVersion(),
    144134            'db' => $this->getDbVersion(),
     
    146136    }
    147137
    148     public function getCallbacks() {
     138    public function getCallbacks()
     139    {
    149140        return [
    150141            'callback' => [
     
    167158    }
    168159
    169     public function callback() {
    170         $apikey = (string)get_option( 'relevatracking_api_key' );
    171         $client_id = (string)get_option( 'relevatracking_client_id' );
    172         $auth = isset($_GET['auth'])?$_GET['auth']:'';
    173         $page = isset($_GET['page'])?$_GET['page']:0;
    174 
    175         if( $auth != md5( $apikey.':'.$client_id ) )
    176         {
     160    public function callback()
     161    {
     162        $apikey = (string)get_option('relevatracking_api_key');
     163        $client_id = (string)get_option('relevatracking_client_id');
     164        $auth = isset($_GET['auth']) ? $_GET['auth'] : '';
     165        $page = isset($_GET['page']) ? $_GET['page'] : 0;
     166
     167        if ($auth != md5($apikey . ':' . $client_id)) {
    177168            exit;
    178169        }
     
    184175
    185176        $wc_version = '';
    186         if( in_array( 'woocommerce/woocommerce.php', apply_filters( 'active_plugins', get_option( 'active_plugins' ) ) ) )
    187         {
     177        if (in_array('woocommerce/woocommerce.php', apply_filters('active_plugins', get_option('active_plugins')))) {
    188178            $wc_version = WC_VERSION;
    189179        }
     
    197187            'callbacks' => $this->getCallbacks(),
    198188        ];
    199         echo json_encode( $callback );
     189        echo json_encode($callback);
    200190
    201191        echo ob_get_clean();
     
    204194
    205195
    206     public function csvexport() {
    207         $apikey = (string)get_option( 'relevatracking_api_key' );
    208         $client_id = (string)get_option( 'relevatracking_client_id' );
    209         $auth = isset($_GET['auth'])?$_GET['auth']:'';
    210         $page = isset($_GET['page'])?$_GET['page']:0;
    211         $limit = isset($_GET['limit'])?$_GET['limit']:500;
    212 
    213         if( $auth != md5( $apikey.':'.$client_id ) )
    214         {
     196    public function csvexport()
     197    {
     198        $apikey = (string)get_option('relevatracking_api_key');
     199        $client_id = (string)get_option('relevatracking_client_id');
     200
     201        $auth = isset($_GET['auth']) ? $_GET['auth'] : '';
     202        $page = isset($_GET['page']) ? $_GET['page'] : 0;
     203        $limit = isset($_GET['limit']) ? $_GET['limit'] : 500;
     204
     205        if ($auth != md5($apikey . ':' . $client_id)) {
    215206            exit;
    216207        }
    217208
    218         $args = array( 
    219             'status' => 'publish', 
    220             'limit' => -1, 
    221             'orderby' => 'id', 
     209        $args = array(
     210            'status' => 'publish',
     211            'limit' => -1,
     212            'orderby' => 'id',
    222213            'order' => 'asc'
    223214        );
    224215
    225         /*
    226         $args = array(
    227             'post_status'   => array('publish'),
    228             'fields'        => 'ids',
    229             'posts_per_page'=> -1,
    230             'post_type'     => array('product'),
    231             'orderby' => 'id',
    232             'order' => 'asc',
    233         );
    234         */
    235 
    236         if( $page > 0 )
    237         {
     216        if ($page > 0) {
    238217            $args['limit'] = $limit;
    239218            $args['page'] = $page;
    240219        }
    241220
    242         $products = wc_get_products( $args );
    243 
    244         if(count($products) == 0) {
     221        $products = wc_get_products($args);
     222
     223        if (count($products) == 0) {
    245224            header('HTTP/1.0 404 Not Found');
    246             exit;
     225            exit;
    247226        }
    248227
     
    255234
    256235        header('Content-Type: text/csv');
    257         header('X-Relevanz-Product-Count: '.$the_query->found_posts);
     236        //header('X-Relevanz-Product-Count: ' . $the_query->found_posts);
    258237        $op = fopen("php://output", "wb");
    259         $header = ['id','categoryIds','name','descriptionShort','descriptionLong','price','priceOffer','link','image','lastUpdate'];
     238        $header = ['id', 'categoryIds', 'name', 'descriptionShort', 'descriptionLong', 'price', 'priceOffer', 'priceNet', 'priceNetOffer', 'link', 'image', 'lastUpdate'];
    260239        fputcsv($op, $header, ',', '"');
    261240
    262 
    263             //while ( $the_query->have_posts() ) {
    264             //$the_query->the_post();
    265             foreach ($products as $p) {
    266                 $product_id = $p->id;
    267                 //$the_query->post->ID = $product_id
    268 
    269                 $single_product = array();
    270                 $product = wc_get_product($product_id);
    271                 if(empty($product) ) {
    272                     continue;
     241        foreach ($products as $p) {
     242            $product_id = $p->id;
     243
     244            $single_product = array();
     245            $product = wc_get_product($product_id);
     246
     247            if (empty($product)) {
     248                continue;
     249            }
     250           
     251            $single_product['id'] = $product_id;
     252
     253            $post_categories = wp_get_post_terms($product_id, $taxonomy = 'product_cat');
     254            $cat = '';
     255            $ii = 0;
     256            foreach ((array)$post_categories as $post_category) :
     257                if ($ii > 0) {
     258                    $cat .= ',';
    273259                }
    274                 $single_product['id'] = $product_id;
    275                 //$single_product['product_id'] = $product->get_id();
    276 
    277                 $post_categories = wp_get_post_terms($product_id, $taxonomy = 'product_cat');
    278                 $cat = ''; $ii = 0;
    279                 foreach((array)$post_categories as $post_category):
    280                     if($ii > 0){$cat .= ',';}
    281                     //$cat .= $post_category->name;
    282                     $cat .= $post_category->term_id;
    283                     $ii++;
    284                 endforeach;
    285                 $single_product['categoryIds'] = $cat;
    286 
    287                 $single_product['name'] = $product->get_name();
    288                 $single_product['descriptionShort'] = $product->get_short_description();
    289                 //$single_product['descriptionLong'] = $product->get_description();
    290                 $single_product['descriptionLong'] = '';
    291 
    292 
    293                 $single_product['price'] = wc_get_price_including_tax( $product, array('price' => $product->get_regular_price() ) );
    294                 //number_format( floatval( get_post_meta( $product_id, '_regular_price', true ) ), 2, '.', '' );
    295                 $single_product['priceOffer'] = wc_get_price_including_tax( $product, array('price' => $product->get_sale_price() ) );
    296                 //( get_post_meta( $product_id, '_sale_price', true ) ? number_format( floatval( get_post_meta( $product_id, '_sale_price', true ) ), 2, '.', '' ) : number_format( floatval( get_post_meta( $product_id, '_regular_price', true ) ), 2, '.', '' ) );
    297 
    298                 $single_product['link'] = $product->get_permalink();
    299 
    300                 $single_product['image'] = $this->get_images( $product )[0];
    301 
    302                 $lastUpdate = get_the_modified_time( 'U', $product_id );
    303                 if( empty( $lastUpdate ) )
    304                 {
    305                     $lastUpdate = get_the_time( 'U', $product_id );
    306                 }
    307                 $single_product['lastUpdate'] = $lastUpdate;
    308 
    309                 fputcsv( $op, $single_product, ',', '"' );
    310             }
     260                $cat .= $post_category->term_id;
     261                $ii++;
     262            endforeach;
     263            $single_product['categoryIds'] = $cat;
     264
     265            $single_product['name'] = $product->get_name();
     266            $single_product['descriptionShort'] = $product->get_short_description();
     267            $single_product['descriptionLong'] = '';
     268
     269
     270            $single_product['price'] = wc_get_price_including_tax($product, array('price' => $product->get_regular_price()));
     271            $single_product['priceOffer'] = wc_get_price_including_tax($product, array('price' => $product->get_sale_price()));
     272            $single_product['priceNet'] = wc_get_price_excluding_tax($product, array('price' => $product->get_regular_price()));
     273            $single_product['priceNetOffer'] = wc_get_price_excluding_tax($product, array('price' => $product->get_sale_price()));         
     274           
     275            $single_product['link'] = $product->get_permalink();
     276
     277            $single_product['image'] = $this->get_images($product)[0];
     278
     279            $lastUpdate = get_the_modified_time('U', $product_id);
     280            if (empty($lastUpdate)) {
     281                $lastUpdate = get_the_time('U', $product_id);
     282            }
     283            $single_product['lastUpdate'] = $lastUpdate;
     284
     285            fputcsv($op, $single_product, ',', '"');
     286        }
    311287
    312288        fclose($op);
     
    323299     * @return array
    324300     */
    325     private function get_images( $product ) {
     301    private function get_images($product)
     302    {
    326303        $images        = $attachment_ids = array();
    327304        $product_image = $product->get_image_id();
    328305
    329306        // Add featured image.
    330         if ( ! empty( $product_image ) ) {
     307        if (!empty($product_image)) {
    331308            $attachment_ids[] = $product_image;
    332309        }
    333310
    334311        // add gallery images.
    335         $attachment_ids = array_merge( $attachment_ids, $product->get_gallery_image_ids() );
     312        $attachment_ids = array_merge($attachment_ids, $product->get_gallery_image_ids());
    336313
    337314        // Build image data.
    338         foreach ( $attachment_ids as $position => $attachment_id ) {
    339 
    340             $attachment_post = get_post( $attachment_id );
    341 
    342             if ( is_null( $attachment_post ) ) {
     315        foreach ($attachment_ids as $position => $attachment_id) {
     316
     317            $attachment_post = get_post($attachment_id);
     318
     319            if (is_null($attachment_post)) {
    343320                continue;
    344321            }
    345322
    346             $attachment = wp_get_attachment_image_src( $attachment_id, 'full' );
    347 
    348             if ( ! is_array( $attachment ) ) {
     323            $attachment = wp_get_attachment_image_src($attachment_id, 'full');
     324
     325            if (!is_array($attachment)) {
    349326                continue;
    350327            }
    351328
    352             $images[] = current( $attachment );
     329            $images[] = current($attachment);
    353330        }
    354331
    355332        // Set a placeholder image if the product has no images set.
    356         if ( empty( $images ) ) {
     333        if (empty($images)) {
    357334
    358335            $images[] = wc_placeholder_img_src();
     
    361338        return $images;
    362339    }
    363 
    364 
    365   /**
    366    * Returns thumbnail if it exists, if not, returns the WC placeholder image
    367    * @param int $id
    368    * @return string
    369    */
    370   private function get_one_thumbnail($id){
    371     $image = false;
    372     $thumb_id = get_post_thumbnail_id( $id );
    373 
    374     if( $thumb_id )
    375       $image = wp_get_attachment_image_src( $thumb_id, 'shop_thumbnail' );
    376 
    377     if( is_array($image) )
    378       return $image[0];
    379 
    380     return wc_placeholder_img_src();
    381   }
    382340
    383341
     
    410368        $result = null;
    411369
    412         if (isset($array[$name]))
    413         {
     370        if (isset($array[$name])) {
    414371            $result = $array[$name];
    415372        }
    416373
    417374        // Handle the default case
    418         if (is_null($result))
    419         {
     375        if (is_null($result)) {
    420376            $result = $default;
    421377        }
    422378
    423379        // Handle the type constraint
    424         switch (strtoupper($type))
    425         {
     380        switch (strtoupper($type)) {
    426381            case 'INT':
    427382            case 'INTEGER':
     
    444399
    445400            case 'ARRAY':
    446                 if (!is_array($result))
    447                 {
     401                if (!is_array($result)) {
    448402                    $result = array($result);
    449403                }
     
    466420    }
    467421
    468 
    469     protected $client_id ;
    470     public $url_js ;
     422    protected $client_id;
     423    protected $additional_html;
    471424
    472425    // Add tracking for pages:
    473     public function relevatracking() {
     426    public function relevatracking()
     427    {
    474428        /**
    475429         * Don't initialize the plugin when WooCommerce is not active.
    476430         */
    477         if ( ! class_exists( 'WooCommerce', false ) ) {
     431        if (!class_exists('WooCommerce', false)) {
    478432            return;
    479433        }
    480434
    481435        // is there any option client_id
    482         if($this->client_id) {
     436        if ($this->client_id) {
     437            $additional_html = (string)get_option('relevatracking_additional_html');
     438            if(!$additional_html) {
     439                $this->additional_html = '<script type="text/javascript">var relevanzAppForcePixel = true;</script>';
     440            } else {
     441                $this->additional_html = $additional_html;
     442            }
    483443            // FRONT_PAGE
    484444            $this->retargeting_front_page();
     
    488448            $this->retargeting_product();
    489449            // PRODUCT
    490             $this->retargeting_cart();         
    491             // ORDER SUCCESS PAGE
    492             $this->retargeting_confirmation();
     450            $this->retargeting_cart();
     451
    493452            // ANY OTHER PAGE
    494453            $this->retargeting_other();
     
    496455    }
    497456
    498 
    499457    // FRONT PAGE Index page
    500     public function retargeting_front_page() {
     458    public function retargeting_front_page()
     459    {
    501460        $user_id = get_current_user_id();
    502461
    503    // URL:  https://pix.hyj.mobi/rt?t=d&action=s&cid=CLIENT_ID
    504         if ( is_front_page() ) {
    505             $this->url_js='https://pix.hyj.mobi/rt?t=d&action=s&cid='.$this->client_id;
    506             if($user_id != 0) {
    507                 $this->url_js .= '&customer_id='.$user_id;
    508             }
    509             echo $this->render( 'front-page' );
    510         }
    511     }
    512 
    513     // CATEGORY PAGE
    514     public function retargeting_category() {
     462        // URL:  https://pix.hyj.mobi/rt?t=d&action=s&cid=CLIENT_ID
     463        if (is_front_page()) {
     464            $url = 'https://pix.hyj.mobi/rt?t=d&action=s&cid=' . $this->client_id;
     465            if ($user_id != 0) {
     466                $url .= '&custid=' . $user_id;
     467            }
     468            $this->addTrackingCode($url);
     469        }
     470    }
     471
     472    // CATEGORY PAGE
     473    public function retargeting_category()
     474    {
    515475        $user_id = get_current_user_id();
    516476
    517477        // URL:  https://pix.hyj.mobi/rt?t=d&action=c&cid=CLIENT_ID&id=CATEGORY_ID
    518         //echo "<pre>is_product_taxonomy"; var_export(is_product_taxonomy() ); echo "</pre>";
    519         if ( function_exists('is_product_category') && is_product_category() ) {
    520          global $wp_query;
    521          // get the query object
    522          $cat = $wp_query->get_queried_object();
    523          if(!empty($cat) && is_object($cat)) {
    524             $id = $cat->term_id;
    525             $this->url_js='https://pix.hyj.mobi/rt?t=d&action=c&cid='.$this->client_id.'&id='.$id;
    526             if($user_id != 0) {
    527                 $this->url_js .= '&customer_id='.$user_id;
    528             }
    529             echo $this->render( 'front-page' );
    530          }
    531         }
    532     }
    533 
    534     // CATEGORY PAGE
    535     public function retargeting_cart() {
     478        if (function_exists('is_product_category') && is_product_category()) {
     479            global $wp_query;
     480            // get the query object
     481            $cat = $wp_query->get_queried_object();
     482            if (!empty($cat) && is_object($cat)) {
     483                $id = $cat->term_id;
     484                $url = 'https://pix.hyj.mobi/rt?t=d&action=c&cid=' . $this->client_id . '&id=' . $id;
     485                if ($user_id != 0) {
     486                    $url .= '&custid=' . $user_id;
     487                }
     488                $this->addTrackingCode($url);
     489            }
     490        }
     491    }
     492
     493    // CATEGORY PAGE
     494    public function retargeting_cart()
     495    {
    536496        $user_id = get_current_user_id();
    537497
    538498        // URL:  https://pix.hyj.mobi/rt?t=d&action=w&cid=CLIENT_ID
    539         if (is_cart() ) {
    540             $this->url_js='https://pix.hyj.mobi/rt?t=d&action=w&cid='.$this->client_id;
    541             if($user_id != 0) {
    542                 $this->url_js .= '&customer_id='.$user_id;
    543             }
    544             echo $this->render( 'front-page' );         
    545         }
    546     }   
     499        if (is_cart()) {
     500            $url = 'https://pix.hyj.mobi/rt?t=d&action=w&cid=' . $this->client_id;
     501            if ($user_id != 0) {
     502                $url .= '&custid=' . $user_id;
     503            }
     504            $this->addTrackingCode($url);
     505        }
     506    }
    547507
    548508    // PRODUCT PAGE
    549     public function retargeting_product() {
     509    public function retargeting_product()
     510    {
    550511        $user_id = get_current_user_id();
    551512
    552     //URL:  https://pix.hyj.mobi/rt?t=d&action=p&cid=CLIENT_ID&id=PRODUCT_ID
    553         if ( is_product() ) {
     513        //URL:  https://pix.hyj.mobi/rt?t=d&action=p&cid=CLIENT_ID&id=PRODUCT_ID
     514        if (is_product()) {
    554515            global $product;
    555             $id = $product->get_id();
    556             if(!$id) {
    557             $id = get_the_ID();
    558             }
    559             $this->url_js='https://pix.hyj.mobi/rt?t=d&action=p&cid='.$this->client_id.'&id='.$id;
    560             if($user_id != 0) {
    561                 $this->url_js .= '&customer_id='.$user_id;
    562             }
    563             echo $this->render( 'front-page' );
     516            $id = $product->get_id();
     517            if (!$id) {
     518                $id = get_the_ID();
     519            }
     520            $url = 'https://pix.hyj.mobi/rt?t=d&action=p&cid=' . $this->client_id . '&id=' . $id;
     521            if ($user_id != 0) {
     522                $url .= '&custid=' . $user_id;
     523            }
     524            $this->addTrackingCode($url);
    564525        }
    565526    }
     
    567528
    568529    // Order Success page
    569     public function retargeting_confirmation() {
     530    public function retargeting_confirmation($order_id)
     531    {
    570532        $user_id = get_current_user_id();
    571 /*
     533        /*
    572534    URL:  https://d.hyj.mobi/convNetw?cid=CLIENT_ID&orderId=ORDER_ID&amount=ORDER_TOTAL&eventName=ARTILE_ID1,ARTILE_ID2,ARTILE_ID3&network=relevanz
    573535*/
    574         if ( is_order_received_page() ) {
    575 
    576             $this->load_confirmation_order_id();
    577             $eventname='';
    578             if(count($this->product_ids)) {
     536        $this->load_confirmation_order_id();
     537        $eventname = '';
     538        if (count($this->product_ids)) {
    579539            //$eventname = json_encode(implode(',',$this->products_name));
    580             $eventname = implode(',',$this->product_ids);
    581             }
    582 
    583             $this->url_js='https://d.hyj.mobi/convNetw?cid='.$this->client_id.'&orderId='.$this->order_id.'&amount='.$this->order_total.'&eventName='.$eventname.'&network=relevanz';
    584             if($user_id != 0) {
    585                 $this->url_js .= '&customer_id='.$user_id;
    586             }
    587 
    588             //echo "<pre>"; var_export($this->url_js); echo "</pre>";
    589             //exit;
    590             echo $this->render( 'front-page' );
    591         }
    592     }
    593 
    594     public function retargeting_other() {
     540            $eventname = implode(',', $this->product_ids);
     541        }
     542
     543        $url = 'https://d.hyj.mobi/convNetw?cid=' . $this->client_id . '&orderId=' . $this->order_id . '&amount=' . $this->order_total . '&eventName=' . $eventname . '&network=relevanz';
     544        if ($user_id != 0) {
     545            $url .= '&custid=' . $user_id;
     546        }
     547
     548        $additional_html = (string)get_option('relevatracking_additional_html');
     549        if(!$additional_html) {
     550            $this->additional_html = '<script type="text/javascript">var relevanzAppForcePixel = true;</script>';
     551        } else {
     552            $this->additional_html = $additional_html;
     553        }
     554        $this->addTrackingCode($url);
     555    }
     556
     557
     558    public function retargeting_other()
     559    {
    595560        $user_id = get_current_user_id();
    596561
    597         if(!is_front_page() && !(function_exists('is_product_category') && is_product_category()) && !is_product() && !is_order_received_page() && !is_cart()) {
    598             $this->url_js='https://pix.hyj.mobi/rt?t=d&action=s&cid='.$this->client_id;
    599             if($user_id != 0) {
    600                 $this->url_js .= '&customer_id='.$user_id;
    601             }
    602             echo $this->render( 'front-page' );         
    603         }
    604     }
    605 
    606     protected $order_id ;
     562        if (!is_front_page() && !(function_exists('is_product_category') && is_product_category()) && !is_product() && !is_order_received_page() && !is_cart()) {
     563            $url = 'https://pix.hyj.mobi/rt?t=d&action=s&cid=' . $this->client_id;
     564            if ($user_id != 0) {
     565                $url .= '&custid=' . $user_id;
     566            }
     567            $this->addTrackingCode($url);
     568        }
     569    }
     570
     571    protected $order_id;
    607572    protected $products_name = array();
    608573    protected $product_ids = array();
    609     protected $order_total ;
     574    protected $order_total;
    610575
    611576    // get Order Success page data
    612     private function load_confirmation_order_id() {
    613         $key=isset($_GET['key'])?$_GET['key']:null;
    614         $this->order_id = wc_get_order_id_by_order_key( $key );
    615         //wc_order_59576109b13fa
    616         if($this->order_id) {
    617         $order = new \WC_Order( $this->order_id );
    618         $this->order_id = $order->get_order_number();
    619         $this->order_total = number_format( (float) $order->get_total() - $order->get_total_tax() - $order->get_total_shipping(), wc_get_price_decimals(), '.', '' );
    620         foreach ( $order->get_items() as $item ) {
    621 
    622             if ( $item['variation_id'] ) {
    623                 $product = new \WC_Product_Variation( $item['variation_id'] );
    624                 $id = $product->get_parent_id();
    625             } else {
    626                 $product = new \WC_Product( $item['product_id'] );
    627                 $id = $product->get_id();
    628             }
    629             $this->products_name[] = $product->get_name();
    630             $this->product_ids[] = $id;
    631         }
    632         return true;
     577    private function load_confirmation_order_id()
     578    {
     579        $key = isset($_GET['key']) ? $_GET['key'] : null;
     580        $this->order_id = wc_get_order_id_by_order_key($key);
     581
     582        if ($this->order_id) {
     583            $order = new \WC_Order($this->order_id);
     584            $this->order_id = $order->get_order_number();
     585            $this->order_total = number_format((float) $order->get_total() - $order->get_total_tax() - $order->get_total_shipping(), wc_get_price_decimals(), '.', '');
     586            foreach ($order->get_items() as $item) {
     587
     588                if ($item['variation_id']) {
     589                    $product = new \WC_Product_Variation($item['variation_id']);
     590                    $id = $product->get_parent_id();
     591                } else {
     592                    $product = new \WC_Product($item['product_id']);
     593                    $id = $product->get_id();
     594                }
     595                $this->products_name[] = $product->get_name();
     596                $this->product_ids[] = $id;
     597            }
     598            return true;
    633599        }
    634600        return false;
    635601    }
    636602
    637     private function escapeJavaScriptText($string)
    638         {
    639             return str_replace("\n", '\n', str_replace('"', '\"', addcslashes(str_replace("\r", '', (string)$string), "\0..\37'\\")));
    640         }
    641 
    642 
    643     public function load_current_category_id() {
     603
     604    public function load_current_category_id()
     605    {
    644606
    645607        global $wp_query;
     
    648610    }
    649611
    650     public function load_category_products_id() {
     612    public function load_category_products_id()
     613    {
    651614
    652615        global $wp_query;
    653616        $this->products_id = array();
    654         foreach ( $wp_query->posts as $product_post ) {
    655             $product = new \WC_Product( $product_post->ID );
     617        foreach ($wp_query->posts as $product_post) {
     618            $product = new \WC_Product($product_post->ID);
    656619            $this->products_id[] = $product->get_sku();
    657620        }
    658621    }
    659622
    660     public function load_product_categories_id() {
     623    public function load_product_categories_id()
     624    {
    661625
    662626        global $post;
    663         $product_categories = get_the_terms( $post->ID, 'product_cat' );
    664         $this->product_categories_id = array_keys( $product_categories );
    665     }
    666 
    667     public function load_product_id() {
     627        $product_categories = get_the_terms($post->ID, 'product_cat');
     628        $this->product_categories_id = array_keys($product_categories);
     629    }
     630
     631    public function load_product_id()
     632    {
    668633
    669634        global $post;
    670         $product = new \WC_Product( $post->ID );
     635        $product = new \WC_Product($post->ID);
    671636        $this->product_id = $product->get_sku();
    672637    }
    673638
    674     public function load_product_price() {
     639    public function load_product_price()
     640    {
    675641
    676642        global $product;
     
    678644    }
    679645
    680     public function load_cart_products_id() {
     646    public function load_cart_products_id()
     647    {
    681648
    682649        $this->products_id = array();
    683         foreach ( WC()->cart->get_cart() as $cart_item ) {
     650        foreach (WC()->cart->get_cart() as $cart_item) {
    684651            $this->products_id[] = $cart_item['data']->get_sku();
    685652        }
    686653    }
    687654
    688     public function load_cart_products_price() {
     655    public function load_cart_products_price()
     656    {
    689657
    690658        $this->cart_products_price = array();
    691         foreach ( WC()->cart->get_cart() as $cart_item ) {
     659        foreach (WC()->cart->get_cart() as $cart_item) {
    692660            $this->cart_products_price[] = $cart_item['data']->get_price();
    693661        }
    694662    }
    695663
    696     public function load_cart_total() {
     664    public function load_cart_total()
     665    {
    697666
    698667        $this->cart_total = WC()->cart->cart_contents_total;
     
    700669
    701670
    702     public function load_confirmation_products_price() {
    703 
    704         $order_id = wc_get_order_id_by_order_key( $_GET['key'] );
    705         $order = new \WC_Order( $order_id );
     671    public function load_confirmation_products_price()
     672    {
     673
     674        $order_id = wc_get_order_id_by_order_key($_GET['key']);
     675        $order = new \WC_Order($order_id);
    706676
    707677        $this->products_price = array();
    708         foreach ( $order->get_items() as $item ) {
    709 
    710             if ( $item['variation_id'] ) {
    711                 $product = new \WC_Product_Variation( $item['variation_id'] );
     678        foreach ($order->get_items() as $item) {
     679
     680            if ($item['variation_id']) {
     681                $product = new \WC_Product_Variation($item['variation_id']);
    712682            } else {
    713                 $product = new \WC_Product( $item['product_id'] );
     683                $product = new \WC_Product($item['product_id']);
    714684            }
    715685            $this->products_price[] = $product->get_price();
     
    717687    }
    718688
    719     public function load_currency() {
     689    public function load_currency()
     690    {
    720691
    721692        $this->currency = 'EUR';
    722693    }
    723694
    724     public function load_new_customer() {
     695    public function load_new_customer()
     696    {
    725697
    726698        global $current_user;
    727699
    728700        $this->new_customer = 0;
    729         if ( isset( $current_user->ID ) ) {
     701        if (isset($current_user->ID)) {
    730702            $args = array();
    731703            $args['post_type'] = 'shop_order';
     
    734706            $args['meta_key'] = '_customer_user';
    735707            $args['meta_value'] = $current_user->ID;
    736             $wp_query = new \WP_Query( $args );
    737             $orders_id = wp_list_pluck( $wp_query->posts, 'ID' );
     708            $wp_query = new \WP_Query($args);
     709            $orders_id = wp_list_pluck($wp_query->posts, 'ID');
    738710            wp_reset_query();
    739711            // First order
    740             if ( count( $orders_id ) == 1 ) {
     712            if (count($orders_id) == 1) {
    741713                $this->new_customer = 1;
    742714            };
     
    744716    }
    745717
    746     public function render( $view_file ) {
    747 
    748         ob_start();
    749         include plugin_dir_path( __FILE__ ) . 'partials/' . $view_file . '.php';
    750         $result = ob_get_contents();
    751         ob_end_clean();
    752         return $result;
    753     }
    754 
    755     /**
    756      * Register the stylesheets for the public-facing side of the site.
    757      *
    758      * @since    1.0.0
    759      */
    760     public function enqueue_styles() {
    761 
    762         /**
    763          * This function is provided for demonstration purposes only.
    764          *
    765          * An instance of this class should be passed to the run() function
    766          * defined in Relevatracking_Loader as all of the hooks are defined
    767          * in that particular class.
    768          *
    769          * The Relevatracking_Loader will then create the relationship
    770          * between the defined hooks and the functions defined in this
    771          * class.
    772          */
    773 
    774         wp_enqueue_style( $this->plugin_name, plugin_dir_url( __FILE__ ) . 'css/relevatracking-public.css', array(), $this->version, 'all' );
    775 
    776     }
    777 
    778     /**
    779      * Register the JavaScript for the public-facing side of the site.
    780      *
    781      * @since    1.0.0
    782      */
    783     public function enqueue_scripts() {
    784 
    785         /**
    786          * This function is provided for demonstration purposes only.
    787          *
    788          * An instance of this class should be passed to the run() function
    789          * defined in Relevatracking_Loader as all of the hooks are defined
    790          * in that particular class.
    791          *
    792          * The Relevatracking_Loader will then create the relationship
    793          * between the defined hooks and the functions defined in this
    794          * class.
    795          */
    796 
    797         wp_enqueue_script( $this->plugin_name, plugin_dir_url( __FILE__ ) . 'js/relevatracking-public.js', array( 'jquery' ), $this->version, false );
    798 
    799     }
    800 
     718    public function addTrackingCode($url) {
     719        // add base code
     720        wp_enqueue_script($this->plugin_name, plugin_dir_url(__FILE__) . 'js/relevatracking-public.js', array(), $this->version, false);
     721        // add url
     722        wp_add_inline_script($this->plugin_name, 'var relevanzURL = "' . $url . '";', 'before');   
     723        // add additional html
     724        wp_add_inline_script($this->plugin_name, $this->additional_html, 'before');
     725    }
    801726}
  • releva-nz/trunk/public/js/relevatracking-public.js

    r1812925 r2988403  
    1 (function( $ ) {
    2     'use strict';
    3 
    4     /**
    5      * All of the code for your public-facing JavaScript source
    6      * should reside in this file.
    7      *
    8      * Note: It has been assumed you will write jQuery code here, so the
    9      * $ function reference has been prepared for usage within the scope
    10      * of this function.
    11      *
    12      * This enables you to define handlers, for when the DOM is ready:
    13      *
    14      * $(function() {
    15      *
    16      * });
    17      *
    18      * When the window is loaded:
    19      *
    20      * $( window ).load(function() {
    21      *
    22      * });
    23      *
    24      * ...and/or other possibilities.
    25      *
    26      * Ideally, it is not considered best practise to attach more than a
    27      * single DOM-ready or window-load handler for a particular page.
    28      * Although scripts in the WordPress core, Plugins and Themes may be
    29      * practising this, we should strive to set a better example in our own work.
    30      */
    31 
    32 })( jQuery );
     1var relevanzRetargetingForcePixelInterval = window.setInterval(function () {
     2            if (typeof relevanzAppForcePixel !== "undefined" && relevanzAppForcePixel === true) {
     3                window.clearInterval(relevanzRetargetingForcePixelInterval);
     4                var script = document.createElement('script');
     5                script.type = 'text/javascript';
     6                script.src = relevanzURL;
     7                document.body.appendChild(script);
     8            }
     9        }, 500);
  • releva-nz/trunk/relevatracking.php

    r2747436 r2988403  
    1717 * Plugin URI:        https://releva.nz
    1818 * Description:       Technology for personalized advertising
    19  * Version:           2.1.3
     19 * Version:           2.1.5
    2020 * Author:            releva.nz
    2121 * License:           GPL-2.0+
Note: See TracChangeset for help on using the changeset viewer.