Plugin Directory

Changeset 2624603


Ignore:
Timestamp:
11/04/2021 04:08:00 PM (4 years ago)
Author:
relevanz
Message:

add internal export functionality

Location:
releva-nz/trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • releva-nz/trunk/public/class-relevatracking-public.php

    r2597152 r2624603  
    5757        // http://localhost/patricianic.domain.wp/?releva_action=jsonexport (json export product)
    5858        add_action( 'releva_jsonexport', array( $this, 'jsonexport' ) );
     59        add_action( 'releva_csvexport', array( $this, 'csvexport' ) );
     60        add_action( 'releva_callback', array( $this, 'callback' ) );
    5961
    6062    }
     
    114116       }
    115117
    116     // http://localhost/patricianic.domain.wp/?releva_action=jsonexport (json export product)
     118    public function getPhpVersion() {
     119        return [
     120            'version' => phpversion(),
     121            'sapi-name' => php_sapi_name(),
     122            'memory-limit' => ini_get( 'memory_limit' ),
     123            'max-execution-time' => ini_get( 'max_execution_time' ),
     124        ];
     125    }
     126
     127    public function getDbVersion() {
     128        global $wpdb;
     129
     130        $version_comment = $wpdb->get_var( 'SELECT @@version_comment AS `server`' );
     131        return [
     132            'version' => $wpdb->db_server_info(),
     133            'server' => $version_comment,
     134        ];
     135    }
     136
     137    public function getServerEnvironment() {
     138        return [
     139            'server-software' => isset( $_SERVER['SERVER_SOFTWARE'] ) ? $_SERVER['SERVER_SOFTWARE'] : null,
     140            'php' => $this->getPhpVersion(),
     141            'db' => $this->getDbVersion(),
     142        ];
     143    }
     144
     145    public function getCallbacks() {
     146        return [
     147            'callback' => [
     148                'url' => site_url('?releva_action=callback'),
     149                'parameters' => [],
     150            ],
     151            'export' => [
     152                'url' => site_url('?releva_action=csvexport'),
     153                'parameters' => [
     154                    'page' => [
     155                        'type' => 'integer',
     156                        'optional' => true,
     157                        'info' => [
     158                            'items-per-page' => 2500,
     159                        ],
     160                    ],
     161                ],
     162            ],
     163        ];
     164    }
     165
     166    public function callback() {
     167        $apikey = (string)get_option( 'relevatracking_api_key' );
     168        $client_id = (string)get_option( 'relevatracking_client_id' );
     169        $auth = isset($_GET['auth'])?$_GET['auth']:'';
     170        $page = isset($_GET['page'])?$_GET['page']:0;
     171
     172        if( $auth != md5( $apikey.':'.$client_id ) )
     173        {
     174            exit;
     175        }
     176
     177        ob_end_flush();
     178        ob_start();
     179
     180        header('Content-Type: application/json');
     181
     182        $wc_version = '';
     183        if( in_array( 'woocommerce/woocommerce.php', apply_filters( 'active_plugins', get_option( 'active_plugins' ) ) ) )
     184        {
     185            $wc_version = WC_VERSION;
     186        }
     187        $callback = [
     188            'plugin-version' => $this->version,
     189            'shop' => [
     190                'system' => 'WooCommerce',
     191                'version' => $wc_version,
     192            ],
     193            'environment' => $this->getServerEnvironment(),
     194            'callbacks' => $this->getCallbacks(),
     195        ];
     196        echo json_encode( $callback );
     197
     198        echo ob_get_clean();
     199        exit;
     200    }
     201
     202    // http://localhost/patricianic.domain.wp/?releva_action=jsonexport (json export product)
    117203    public function jsonexport() {
    118204               $args = array(
     
    178264                    exit;
    179265        }
     266
     267    public function csvexport() {
     268        $apikey = (string)get_option( 'relevatracking_api_key' );
     269        $client_id = (string)get_option( 'relevatracking_client_id' );
     270        $auth = isset($_GET['auth'])?$_GET['auth']:'';
     271        $page = isset($_GET['page'])?$_GET['page']:0;
     272
     273        if( $auth != md5( $apikey.':'.$client_id ) )
     274        {
     275            exit;
     276        }
     277
     278        $args = array(
     279            'post_status'   => array('publish'),
     280            'fields'        => 'ids',
     281            'posts_per_page'=> -1,
     282            'post_type'     => array('product'),
     283            'orderby' => 'id',
     284            'order' => 'asc',
     285        );
     286        if( $page > 0 )
     287        {
     288            $args['posts_per_page'] = 2500;
     289            $args['paged'] = $page;
     290        }
     291
     292        $the_query = new WP_Query( $args );
     293        // The Loop
     294        $numProducts = 0;
     295        global $wpdb;
     296
     297        ob_end_flush();
     298        ob_start();
     299
     300        header('Content-Type: text/csv');
     301        header('X-Relevanz-Product-Count: '.$the_query->found_posts);
     302        $op = fopen("php://output", "wb");
     303        $header = ['id','categoryIds','name','descriptionShort','descriptionLong','price','priceOffer','link','image','lastUpdate'];
     304        fputcsv($op, $header, ',', '"');
     305
     306        if( $the_query->have_posts() )
     307        {
     308            //while ( $the_query->have_posts() ) {
     309            //$the_query->the_post();
     310            foreach ($the_query->posts as $product_id) {
     311
     312                //$the_query->post->ID = $product_id
     313
     314                $single_product = array();
     315                $product = wc_get_product($product_id);
     316                if(empty($product) ) {
     317                    continue;
     318                }
     319                $single_product['id'] = $product_id;
     320                //$single_product['product_id'] = $product->get_id();
     321
     322                $post_categories = wp_get_post_terms($product_id, $taxonomy = 'product_cat');
     323                $cat = ''; $ii = 0;
     324                foreach((array)$post_categories as $post_category):
     325                    if($ii > 0){$cat .= ',';}
     326                    //$cat .= $post_category->name;
     327                    $cat .= $post_category->term_id;
     328                    $ii++;
     329                endforeach;
     330                $single_product['categoryIds'] = $cat;
     331
     332                $single_product['name'] = $product->get_name();
     333                $single_product['descriptionShort'] = $product->get_short_description();
     334                $single_product['descriptionLong'] = $product->get_description();
     335
     336                $single_product['price'] = wc_get_price_including_tax( $product, array('price' => $product->get_regular_price() ) );
     337                //number_format( floatval( get_post_meta( $product_id, '_regular_price', true ) ), 2, '.', '' );
     338                $single_product['priceOffer'] = wc_get_price_including_tax( $product, array('price' => $product->get_sale_price() ) );
     339                //( 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, '.', '' ) );
     340
     341                $single_product['link'] = $product->get_permalink();
     342
     343                $single_product['image'] = $this->get_images( $product )[0];
     344
     345                $lastUpdate = get_the_modified_time( 'U', $product_id );
     346                if( empty( $lastUpdate ) )
     347                {
     348                    $lastUpdate = get_the_time( 'U', $product_id );
     349                }
     350                $single_product['lastUpdate'] = $lastUpdate;
     351
     352                fputcsv( $op, $single_product, ',', '"' );
     353            }
     354
     355
     356        }
     357        fclose($op);
     358
     359        echo ob_get_clean();
     360        exit;
     361    }
    180362
    181363    /**
  • releva-nz/trunk/relevatracking.php

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