Plugin Directory

Changeset 2532343

Timestamp:
05/16/2021 01:45:27 AM (3 years ago)
Author:
wokamoto
Message:

[simple-ga-ranking] Update ver.2.1.1

Location:
simple-ga-ranking
Files:
7 edited
2 copied

Legend:

Unmodified
Added
Removed
  • simple-ga-ranking/tags/2.1.1/admin/admin.php

    r2531557 r2532343  
    11<?php
    22
    3 add_action( 'admin_menu', function () {
     3add_action( 'admin_menu', function ()
     4{
    45    add_options_page(
    56        __( 'Simple GA Ranking', SGA_RANKING_DOMAIN ),
     
    1112});
    1213
    13 function sga_ranking_options_page() {
     14function sga_ranking_options_page()
     15{
    1416    echo '<div class="wrap">';
    1517
     
    1820    echo '<form action="options.php" method="post" id="sga-post">';
    1921
    20     settings_fields( 'sga_ranking_options' );
     22    settings_fields( );
    2123    do_settings_sections( 'sga_ranking' );
    2224    do_meta_boxes( 'sga_ranking', 'advanced', '' );
     
    3537}
    3638
    37 add_action( 'admin_init', function () {
     39add_action( 'admin_init', function ()
     40{
    3841    register_setting(
    39         'sga_ranking_options',
    40         'sga_ranking_options',
     42        ,
     43        ,
    4144        'sga_ranking_options_validate'
    4245    );
     
    6568});
    6669
    67 function sga_ranking_section_text() {
     70function sga_ranking_section_text()
     71{
    6872    do_action( 'sga_ranking_section_text' );
    6973}
    7074
    71 function sga_ranking_setting_period() {
    72     $options = get_option( 'sga_ranking_options' );
     75function sga_ranking_setting_period()
     76{
     77    $options = get_option( SGA_RANKING_OPTION_NAME );
    7378    $option_name = 'period';
    7479
     
    8489}
    8590
    86 function sga_ranking_setting_display_count() {
    87     $options = get_option( 'sga_ranking_options' );
     91function sga_ranking_setting_display_count()
     92{
     93    $options = get_option( SGA_RANKING_OPTION_NAME );
    8894    $option_name = 'display_count';
    8995   
     
    98104}
    99105
    100 function sga_ranking_setting_debug_mode() {
    101     $options = get_option( 'sga_ranking_options' );
     106function sga_ranking_setting_debug_mode()
     107{
     108    $options = get_option( SGA_RANKING_OPTION_NAME );
    102109    $option_name = 'debug_mode';
    103110   
     
    113120}
    114121
    115 function sga_ranking_options_validate( $input ) {
     122function sga_ranking_options_validate( $input )
     123{
    116124    $newinput['period'] = absint( $input['period'] );
    117125    $newinput['display_count'] = absint( $input['display_count'] );
     
    122130}
    123131
    124 add_action( 'admin_notices', function () {
     132add_action( 'admin_notices', function ()
     133{
    125134    $token = get_option('gapiwp_token');
    126135   
  • simple-ga-ranking/tags/2.1.1/lib/wp-rest-api.class.php

    r1086949 r2532343  
    33 * Name: SGRanking Endpoint
    44 */
    5 class WP_JSON_SGRanking extends WP_JSON_Posts {
     5class WP_JSON_SGRanking extends WP_JSON_Posts
     6{
    67
    78    /**
     
    1112     * @return array Modified routes
    1213     */
    13     public function register_routes( $routes ) {
     14    public function register_routes( $routes )
     15    {
    1416        $ranking_routes = array(
    1517            '/ranking'             => array(
     
    2830     * @see WP_JSON_Posts::get_posts()
    2931     */
    30     public function get_ranking( $filter = array(), $context = 'view' ) {
     32    public function get_ranking( $filter = array(), $context = 'view' )
     33    {
    3134        $ids        = sga_ranking_get_date( $filter );
    3235        $posts_list = array();
  • simple-ga-ranking/tags/2.1.1/readme.txt

    r2531567 r2532343  
    44Requires at least: 3.6.1
    55Tested up to: 5.7.2
    6 Stable tag: 2.1
     6Stable tag: 2.1
    77
    88Ranking plugin using data from google analytics.
     
    9292= 2.1 =
    9393* refactoring
     94
     95
  • simple-ga-ranking/tags/2.1.1/simple-ga-ranking.php

    r2531557 r2532343  
    55Plugin URI: http://simple-ga-ranking.org
    66Description: Ranking plugin using data from google analytics.
    7 Version: 2.1
     7Version: 2.1
    88Author URI: http://simple-ga-ranking.org
    99Domain Path: /languages
     
    3131}
    3232if ( ! defined( 'SGA_RANKING_PLUGIN_URL' ) ) {
    33     define( 'SGA_RANKING_PLUGIN_URL', plugins_url() . '/' . dirname( plugin_basename( __FILE__ ) ));
     33    define( 'SGA_RANKING_PLUGIN_URL', plugins_url() . '/' . dirname( plugin_basename( __FILE__ ) ));
    3434}
    3535if ( ! defined( 'SGA_RANKING_PLUGIN_DIR' ) ) {
    36     define( 'SGA_RANKING_PLUGIN_DIR', WP_PLUGIN_DIR . '/' . dirname( plugin_basename( __FILE__ ) ));
     36    define( 'SGA_RANKING_PLUGIN_DIR', WP_PLUGIN_DIR . '/' . dirname( plugin_basename( __FILE__ ) ) );
     37}
     38if ( ! defined( 'SGA_RANKING_OPTION_NAME' ) ) {
     39    define( 'SGA_RANKING_OPTION_NAME', 'sga_ranking_options' );
    3740}
    3841
     
    4548$simple_ga_ranking = \Hametuha\GapiWP\Loader::analytics();
    4649
    47 function sga_ranking_get_date( $args = array() ) {
     50function sga_ranking_get_date( $args = array() )
     51{
     52    return sga_ranking_get_data( $args );
     53}
     54function sga_ranking_get_data( $args = array() )
     55{
    4856    // cache expire time
    4957    $cache_expires = (int) apply_filters( 'sga_ranking_cache_expire', 24*60*60 );
     
    5361
    5462    // get options
    55     $options = get_option( 'sga_ranking_options' );
     63    $options = get_option( );
    5664
    5765    // get args
     
    129137    $debug_mode = ( defined( 'SGA_RANKING_TEST_MODE' ) && SGA_RANKING_TEST_MODE === true ) || ( isset($options['debug_mode']) && $options['debug_mode'] == 1 );
    130138    if ( false === $ids && $debug_mode ) {
    131         global $wpdb;
    132 
    133         $query = $wpdb->prepare(
    134             "SELECT ID FROM {$wpdb->posts} WHERE post_type = %s AND post_status = %s ORDER BY RAND() LIMIT 0, %d",
    135             'post',
    136             'publish',
    137             $options['display_count']
    138         );
    139         $rets = $wpdb->get_results( $query );
    140         $ids = array();
    141         foreach ( $rets as $ret ) {
    142             $ids[] = $ret->ID;
    143         }
    144 
     139        $ids = sga_ranking_get_dummy_data( $args );
    145140        set_transient( $transient_key, $ids, $cache_expires * 2 );
    146141    }
     
    148143    // get GA ranking
    149144    if ( false !== $ids ) {
     145
    150146        $post_ids = $ids;
    151147
    152148    } else {
    153         $post_ids = array();
    154 
    155         $args = array(
     149        // from Google Analytics API
     150        $ga_args = array(
    156151                'start-index' => 1,
    157152                'max-results' => $post_limit,
     
    159154                'sort'        => '-ga:pageviews',
    160155        );
    161         if ( ! empty($filter_val) ) {
    162             $args['filters'] = $filter_val;
     156        if ( ! empty() ) {
     157            $args['filters'] = $filter_val;
    163158        }
    164159        $results = $simple_ga_ranking->fetch(
     
    166161            $options['end_date'],
    167162            'ga:pageviews',
    168             $args
     163            $args
    169164        );
    170165
    171         $cnt = 0;
    172         if ( !empty( $results ) && !is_wp_error( $results ) && is_array( $results->rows ) ) {
     166        if ( ! empty( $results ) && !is_wp_error( $results ) && is_array( $results->rows ) ) {
     167            $post_ids = array();
     168            $cnt = 0;
     169
    173170            foreach($results->rows as $result) {
    174171                $max = (int) $options['display_count'];
     
    192189
    193190                $post_obj = get_post($post_id);
    194                 if ( !is_object($post_obj) || $post_obj->post_status != 'publish' ){
     191                if ( !is_object($post_obj) || $post_obj->post_status != 'publish' ){
    195192                    continue;
    196193                }
    197    
     194
    198195                if ( !empty($r) ) {
    199196                    if ( array_key_exists( 'post_type', $r ) && is_string($r['post_type']) ) {
     
    253250                    }
    254251                }
    255    
     252
    256253                $post_ids[] = $post_id;
    257254                $cnt++;
    258255            }
     256
    259257
    260258        } else {
     259
     260
    261261            if ( is_super_admin() ) {
    262262                echo '<pre>';
     
    265265            }
    266266        }
    267 
    268         set_transient( $transient_key, $post_ids, $cache_expires * 2 );
    269267    }
    270268
    271269    return apply_filters( 'sga_ranking_ids', $post_ids );
     270
     271
     272
     273
     274
     275
     276
     277
     278
     279
     280
     281
     282
     283
     284
     285
     286
     287
     288
     289
     290
     291
     292
     293
     294
    272295}
    273296
    274297add_filter( 'widget_text', 'do_shortcode' );
    275298add_shortcode('sga_ranking', 'sga_ranking_shortcode');
    276 function sga_ranking_shortcode( $atts ) {
    277 
    278     $ids = sga_ranking_get_date($atts);
     299function sga_ranking_shortcode( $atts )
     300{
     301
     302    $ids = sga_ranking_get_data($atts);
    279303
    280304    if ( empty( $ids ) ) {
     
    296320
    297321//widget
    298 class WP_Widget_Simple_GA_Ranking extends WP_Widget {
    299 
    300     function __construct() {
     322class WP_Widget_Simple_GA_Ranking extends WP_Widget
     323{
     324
     325    function __construct()
     326    {
    301327        $widget_ops = array('classname' => 'widget_simple_ga_ranking', 'description' => __( "Show ranking the data from Google Analytics", SGA_RANKING_DOMAIN ) );
    302328        parent::__construct('simple_ga_rankig', __('Simple GA Ranking'), $widget_ops);
    303329    }
    304330
    305     function widget( $args, $instance ) {
     331    function widget( $args, $instance )
     332    {
    306333        extract($args);
    307334        $title = apply_filters( 'widget_title', empty( $instance['title'] ) ? '' : $instance['title'], $instance, $this->id_base );
     
    317344    }
    318345
    319     function form( $instance ) {
     346    function form( $instance )
     347    {
    320348        $instance = wp_parse_args( (array) $instance, array( 'title' => '') );
    321349        $title = $instance['title'];
     
    325353    }
    326354
    327     function update( $new_instance, $old_instance ) {
     355    function update( $new_instance, $old_instance )
     356    {
    328357        $instance = $old_instance;
    329358        $new_instance = wp_parse_args((array) $new_instance, array( 'title' => ''));
     
    475504    require_once( SGA_RANKING_PLUGIN_DIR . '/lib/wp-rest-api.class.php' );
    476505
    477     function sga_json_api_ranking_filters( $server ) {
     506    function sga_json_api_ranking_filters( $server )
     507    {
    478508        // Ranking
    479509        $wp_json_ranking = new WP_JSON_SGRanking( $server );
    480         add_filter( 'json_endpoints', array( $wp_json_ranking, 'register_routes'    ), 1     );
     510        add_filter( 'json_endpoints', array( $wp_json_ranking, 'register_routes'    ), 1 );
    481511    }
    482512    add_action( 'wp_json_server_before_serve', 'sga_json_api_ranking_filters', 10, 1 );
  • simple-ga-ranking/trunk/admin/admin.php

    r2531557 r2532343  
    11<?php
    22
    3 add_action( 'admin_menu', function () {
     3add_action( 'admin_menu', function ()
     4{
    45    add_options_page(
    56        __( 'Simple GA Ranking', SGA_RANKING_DOMAIN ),
     
    1112});
    1213
    13 function sga_ranking_options_page() {
     14function sga_ranking_options_page()
     15{
    1416    echo '<div class="wrap">';
    1517
     
    1820    echo '<form action="options.php" method="post" id="sga-post">';
    1921
    20     settings_fields( 'sga_ranking_options' );
     22    settings_fields( );
    2123    do_settings_sections( 'sga_ranking' );
    2224    do_meta_boxes( 'sga_ranking', 'advanced', '' );
     
    3537}
    3638
    37 add_action( 'admin_init', function () {
     39add_action( 'admin_init', function ()
     40{
    3841    register_setting(
    39         'sga_ranking_options',
    40         'sga_ranking_options',
     42        ,
     43        ,
    4144        'sga_ranking_options_validate'
    4245    );
     
    6568});
    6669
    67 function sga_ranking_section_text() {
     70function sga_ranking_section_text()
     71{
    6872    do_action( 'sga_ranking_section_text' );
    6973}
    7074
    71 function sga_ranking_setting_period() {
    72     $options = get_option( 'sga_ranking_options' );
     75function sga_ranking_setting_period()
     76{
     77    $options = get_option( SGA_RANKING_OPTION_NAME );
    7378    $option_name = 'period';
    7479
     
    8489}
    8590
    86 function sga_ranking_setting_display_count() {
    87     $options = get_option( 'sga_ranking_options' );
     91function sga_ranking_setting_display_count()
     92{
     93    $options = get_option( SGA_RANKING_OPTION_NAME );
    8894    $option_name = 'display_count';
    8995   
     
    98104}
    99105
    100 function sga_ranking_setting_debug_mode() {
    101     $options = get_option( 'sga_ranking_options' );
     106function sga_ranking_setting_debug_mode()
     107{
     108    $options = get_option( SGA_RANKING_OPTION_NAME );
    102109    $option_name = 'debug_mode';
    103110   
     
    113120}
    114121
    115 function sga_ranking_options_validate( $input ) {
     122function sga_ranking_options_validate( $input )
     123{
    116124    $newinput['period'] = absint( $input['period'] );
    117125    $newinput['display_count'] = absint( $input['display_count'] );
     
    122130}
    123131
    124 add_action( 'admin_notices', function () {
     132add_action( 'admin_notices', function ()
     133{
    125134    $token = get_option('gapiwp_token');
    126135   
  • simple-ga-ranking/trunk/lib/wp-rest-api.class.php

    r1086949 r2532343  
    33 * Name: SGRanking Endpoint
    44 */
    5 class WP_JSON_SGRanking extends WP_JSON_Posts {
     5class WP_JSON_SGRanking extends WP_JSON_Posts
     6{
    67
    78    /**
     
    1112     * @return array Modified routes
    1213     */
    13     public function register_routes( $routes ) {
     14    public function register_routes( $routes )
     15    {
    1416        $ranking_routes = array(
    1517            '/ranking'             => array(
     
    2830     * @see WP_JSON_Posts::get_posts()
    2931     */
    30     public function get_ranking( $filter = array(), $context = 'view' ) {
     32    public function get_ranking( $filter = array(), $context = 'view' )
     33    {
    3134        $ids        = sga_ranking_get_date( $filter );
    3235        $posts_list = array();
  • simple-ga-ranking/trunk/readme.txt

    r2531567 r2532343  
    44Requires at least: 3.6.1
    55Tested up to: 5.7.2
    6 Stable tag: 2.1
     6Stable tag: 2.1
    77
    88Ranking plugin using data from google analytics.
     
    9292= 2.1 =
    9393* refactoring
     94
     95
  • simple-ga-ranking/trunk/simple-ga-ranking.php

    r2531557 r2532343  
    55Plugin URI: http://simple-ga-ranking.org
    66Description: Ranking plugin using data from google analytics.
    7 Version: 2.1
     7Version: 2.1
    88Author URI: http://simple-ga-ranking.org
    99Domain Path: /languages
     
    3131}
    3232if ( ! defined( 'SGA_RANKING_PLUGIN_URL' ) ) {
    33     define( 'SGA_RANKING_PLUGIN_URL', plugins_url() . '/' . dirname( plugin_basename( __FILE__ ) ));
     33    define( 'SGA_RANKING_PLUGIN_URL', plugins_url() . '/' . dirname( plugin_basename( __FILE__ ) ));
    3434}
    3535if ( ! defined( 'SGA_RANKING_PLUGIN_DIR' ) ) {
    36     define( 'SGA_RANKING_PLUGIN_DIR', WP_PLUGIN_DIR . '/' . dirname( plugin_basename( __FILE__ ) ));
     36    define( 'SGA_RANKING_PLUGIN_DIR', WP_PLUGIN_DIR . '/' . dirname( plugin_basename( __FILE__ ) ) );
     37}
     38if ( ! defined( 'SGA_RANKING_OPTION_NAME' ) ) {
     39    define( 'SGA_RANKING_OPTION_NAME', 'sga_ranking_options' );
    3740}
    3841
     
    4548$simple_ga_ranking = \Hametuha\GapiWP\Loader::analytics();
    4649
    47 function sga_ranking_get_date( $args = array() ) {
     50function sga_ranking_get_date( $args = array() )
     51{
     52    return sga_ranking_get_data( $args );
     53}
     54function sga_ranking_get_data( $args = array() )
     55{
    4856    // cache expire time
    4957    $cache_expires = (int) apply_filters( 'sga_ranking_cache_expire', 24*60*60 );
     
    5361
    5462    // get options
    55     $options = get_option( 'sga_ranking_options' );
     63    $options = get_option( );
    5664
    5765    // get args
     
    129137    $debug_mode = ( defined( 'SGA_RANKING_TEST_MODE' ) && SGA_RANKING_TEST_MODE === true ) || ( isset($options['debug_mode']) && $options['debug_mode'] == 1 );
    130138    if ( false === $ids && $debug_mode ) {
    131         global $wpdb;
    132 
    133         $query = $wpdb->prepare(
    134             "SELECT ID FROM {$wpdb->posts} WHERE post_type = %s AND post_status = %s ORDER BY RAND() LIMIT 0, %d",
    135             'post',
    136             'publish',
    137             $options['display_count']
    138         );
    139         $rets = $wpdb->get_results( $query );
    140         $ids = array();
    141         foreach ( $rets as $ret ) {
    142             $ids[] = $ret->ID;
    143         }
    144 
     139        $ids = sga_ranking_get_dummy_data( $args );
    145140        set_transient( $transient_key, $ids, $cache_expires * 2 );
    146141    }
     
    148143    // get GA ranking
    149144    if ( false !== $ids ) {
     145
    150146        $post_ids = $ids;
    151147
    152148    } else {
    153         $post_ids = array();
    154 
    155         $args = array(
     149        // from Google Analytics API
     150        $ga_args = array(
    156151                'start-index' => 1,
    157152                'max-results' => $post_limit,
     
    159154                'sort'        => '-ga:pageviews',
    160155        );
    161         if ( ! empty($filter_val) ) {
    162             $args['filters'] = $filter_val;
     156        if ( ! empty() ) {
     157            $args['filters'] = $filter_val;
    163158        }
    164159        $results = $simple_ga_ranking->fetch(
     
    166161            $options['end_date'],
    167162            'ga:pageviews',
    168             $args
     163            $args
    169164        );
    170165
    171         $cnt = 0;
    172         if ( !empty( $results ) && !is_wp_error( $results ) && is_array( $results->rows ) ) {
     166        if ( ! empty( $results ) && !is_wp_error( $results ) && is_array( $results->rows ) ) {
     167            $post_ids = array();
     168            $cnt = 0;
     169
    173170            foreach($results->rows as $result) {
    174171                $max = (int) $options['display_count'];
     
    192189
    193190                $post_obj = get_post($post_id);
    194                 if ( !is_object($post_obj) || $post_obj->post_status != 'publish' ){
     191                if ( !is_object($post_obj) || $post_obj->post_status != 'publish' ){
    195192                    continue;
    196193                }
    197    
     194
    198195                if ( !empty($r) ) {
    199196                    if ( array_key_exists( 'post_type', $r ) && is_string($r['post_type']) ) {
     
    253250                    }
    254251                }
    255    
     252
    256253                $post_ids[] = $post_id;
    257254                $cnt++;
    258255            }
     256
    259257
    260258        } else {
     259
     260
    261261            if ( is_super_admin() ) {
    262262                echo '<pre>';
     
    265265            }
    266266        }
    267 
    268         set_transient( $transient_key, $post_ids, $cache_expires * 2 );
    269267    }
    270268
    271269    return apply_filters( 'sga_ranking_ids', $post_ids );
     270
     271
     272
     273
     274
     275
     276
     277
     278
     279
     280
     281
     282
     283
     284
     285
     286
     287
     288
     289
     290
     291
     292
     293
     294
    272295}
    273296
    274297add_filter( 'widget_text', 'do_shortcode' );
    275298add_shortcode('sga_ranking', 'sga_ranking_shortcode');
    276 function sga_ranking_shortcode( $atts ) {
    277 
    278     $ids = sga_ranking_get_date($atts);
     299function sga_ranking_shortcode( $atts )
     300{
     301
     302    $ids = sga_ranking_get_data($atts);
    279303
    280304    if ( empty( $ids ) ) {
     
    296320
    297321//widget
    298 class WP_Widget_Simple_GA_Ranking extends WP_Widget {
    299 
    300     function __construct() {
     322class WP_Widget_Simple_GA_Ranking extends WP_Widget
     323{
     324
     325    function __construct()
     326    {
    301327        $widget_ops = array('classname' => 'widget_simple_ga_ranking', 'description' => __( "Show ranking the data from Google Analytics", SGA_RANKING_DOMAIN ) );
    302328        parent::__construct('simple_ga_rankig', __('Simple GA Ranking'), $widget_ops);
    303329    }
    304330
    305     function widget( $args, $instance ) {
     331    function widget( $args, $instance )
     332    {
    306333        extract($args);
    307334        $title = apply_filters( 'widget_title', empty( $instance['title'] ) ? '' : $instance['title'], $instance, $this->id_base );
     
    317344    }
    318345
    319     function form( $instance ) {
     346    function form( $instance )
     347    {
    320348        $instance = wp_parse_args( (array) $instance, array( 'title' => '') );
    321349        $title = $instance['title'];
     
    325353    }
    326354
    327     function update( $new_instance, $old_instance ) {
     355    function update( $new_instance, $old_instance )
     356    {
    328357        $instance = $old_instance;
    329358        $new_instance = wp_parse_args((array) $new_instance, array( 'title' => ''));
     
    475504    require_once( SGA_RANKING_PLUGIN_DIR . '/lib/wp-rest-api.class.php' );
    476505
    477     function sga_json_api_ranking_filters( $server ) {
     506    function sga_json_api_ranking_filters( $server )
     507    {
    478508        // Ranking
    479509        $wp_json_ranking = new WP_JSON_SGRanking( $server );
    480         add_filter( 'json_endpoints', array( $wp_json_ranking, 'register_routes'    ), 1     );
     510        add_filter( 'json_endpoints', array( $wp_json_ranking, 'register_routes'    ), 1 );
    481511    }
    482512    add_action( 'wp_json_server_before_serve', 'sga_json_api_ranking_filters', 10, 1 );
Note: See TracChangeset for help on using the changeset viewer.