Plugin Directory

Changeset 2065460


Ignore:
Timestamp:
04/09/2019 02:47:35 AM (7 years ago)
Author:
marcusraket.nu
Message:

Major cleanup of WP_MTPC

Location:
metapic/trunk/classes
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • metapic/trunk/classes/WP_MTPC.php

    r2065457 r2065460  
    66    use WP_MTPC_Utilities;
    77   
    8     private $api_url = "https://api.metapic.se";
    9     private $userapi_url = "https://mtpc.se";
    10     private $cdn_url = "https://api.metapic.se";
     8    private $api_url = 'https://api.metapic.se';
     9    private $userapi_url = 'https://mtpc.se';
     10    private $cdn_url = 'https://api.metapic.se';
    1111    private $plugin_dir;
    1212    private $plugin_url;
     
    2828   
    2929    private $templateVars = [];
    30     private $debugMode = false;
    31     private $isDocker = false;
    32     private $accessKey = "metapic_access_token";
    33     private $tokenUrl = "";
     30    private $debugMode;
     31    private $isDocker;
     32    private $accessKey = 'metapic_access_token';
     33    private $tokenUrl;
    3434    private $autoRegister = false;
    35     private $activeAccount = false;
     35    private $activeAccount;
    3636    private $forceSSL = true;
    3737   
    3838    public function __construct( $plugin_dir, $plugin_url ) {
    39         $this->debugMode = ( ( defined( "MTPC_DEBUG" ) && MTPC_DEBUG === true )
    40                              || ( isset( $_ENV["MTPC_DEBUG"] )
    41                                   && $_ENV["MTPC_DEBUG"] === "true" ) );
    42         $this->isDocker  = ( isset( $_ENV["MTPC_IS_DOCKER"] )
    43                              && $_ENV["MTPC_IS_DOCKER"] === "true" );
     39        $this->debugMode = ( ( defined( 'MTPC_DEBUG' ) && MTPC_DEBUG === true )
     40                             || ( isset( $_ENV['MTPC_DEBUG'] )
     41                                  && $_ENV['MTPC_DEBUG'] === 'true' ) );
     42        $this->isDocker  = ( isset( $_ENV['MTPC_IS_DOCKER'] )
     43                             && $_ENV['MTPC_IS_DOCKER'] === 'true' );
    4444       
    4545        $this->plugin_dir = $plugin_dir;
     
    4848       
    4949        if ( $this->forceSSL || is_ssl() ) {
    50             $this->api_url     = "https://api.metapic.se";
    51             $this->userapi_url = "https://mtpc.se";
    52             $this->cdn_url     = "https://api.metapic.se";
    53         }
    54        
    55         if ( defined( "MTPC_API_URL" ) && $this->debugMode ) {
     50            $this->api_url     = 'https://api.metapic.se';
     51            $this->userapi_url = 'https://mtpc.se';
     52            $this->cdn_url     = 'https://api.metapic.se';
     53        }
     54       
     55        if ( defined( 'MTPC_API_URL' ) && $this->debugMode ) {
    5656            $this->api_url = MTPC_API_URL;
    5757        }
    5858       
    5959        $this->client = new ApiClient( $this->getApiUrl(),
    60           get_site_option( "mtpc_api_key" ),
    61           get_site_option( "mtpc_secret_key" ) );
    62        
    63         $this->tokenUrl = rtrim( get_bloginfo( "url" ), "/" ) . "/?"
     60          get_site_option( 'mtpc_api_key' ),
     61          get_site_option( 'mtpc_secret_key' ) );
     62       
     63        $this->tokenUrl = rtrim( get_bloginfo( 'url' ), '/' ) . '/?'
    6464                          . $this->accessKey;
    6565       
     
    9292            'status'  => 'error'
    9393          ],
    94          
    9594          [
    9695            'type'    => 'mtpc-user-logout',
     
    134133        if ( is_multisite() ) {
    135134            $this->autoRegister
    136               = (bool) get_site_option( "mtpc_registration_auto" );
     135              = (bool) get_site_option( 'mtpc_registration_auto' );
    137136        }
    138137        $this->activeAccount = $this->has_active_account();
     
    140139        $this->settings->handle_admin_ms_settings( $this->getApiUrl() );
    141140        $this->settings->handle_mtpc_user_my_account($this->debugMode);
    142         $this->settings->handle_mtpc_user_new( $this );
     141        $this->settings->handle_mtpc_user_new( );
    143142        $this->settings->handle_mtpc_user_reactivate();
    144143       
     
    158157        add_filter( 'wp_kses_allowed_html', function ( $tags, $context ) {
    159158            foreach ( $tags as $key => $value ) {
    160                 $tags[ $key ]["data-metapic-id"]       = 1;
    161                 $tags[ $key ]["data-metapic-tags"]     = 1;
    162                 $tags[ $key ]["data-metapic-link-url"] = 1;
     159                $tags[ $key ]['data-metapic-id']       = 1;
     160                $tags[ $key ]['data-metapic-tags']     = 1;
     161                $tags[ $key ]['data-metapic-link-url'] = 1;
    163162            }
    164163           
     
    178177    private function setupFrontendJsOptions() {
    179178        $jsHandle = 'mtpc_frontend_js';
    180         add_action( "wp_head", function () use ( $jsHandle ) {
     179        add_action( 'wp_head', function () use ( $jsHandle ) {
    181180            if ( $this->debugMode ) {
    182181               
     
    225224        }, 10 );
    226225       
    227         add_action( "wp_footer", function () {
    228             $this->getTemplate( "metapic-load" );
     226        add_action( 'wp_footer', function () {
     227            $this->getTemplate( 'metapic-load' );
    229228        } );
    230229    }
    231230   
    232231    private function setupBackendJsOptions() {
    233         $baseUrl         = ( $this->isDocker ) ? "http://localhost:3000"
     232        $baseUrl         = $this->isDocker ? 'http://localhost:3000'
    234233          : $this->client->getBaseUrl();
    235         $accessToken     = get_option( "mtpc_access_token" );
    236         $mce_plugin_name = "metapic";
     234        $accessToken     = get_option( 'mtpc_access_token' );
     235        $mce_plugin_name = 'metapic';
    237236       
    238237        // Declare script for new button
     
    263262       
    264263        add_filter( 'tiny_mce_before_init', function ( $mceInit, $editor_id ) {
    265             $mceInit["mtpc_iframe_url"] = $this->tokenUrl;
    266             $mceInit["mtpc_plugin_url"] = $this->plugin_url;
     264            $mceInit['mtpc_iframe_url'] = $this->tokenUrl;
     265            $mceInit['mtpc_plugin_url'] = $this->plugin_url;
    267266            if ( $this->isDocker ) {
    268                 $mceInit["mtpc_base_url"] = "http://localhost:3000";
     267                $mceInit['mtpc_base_url'] = 'http://localhost:3000';
    269268            } else {
    270                 $mceInit["mtpc_base_url"] = $this->client->getBaseUrl();
    271             }
    272             $mceInit["mtpc_access_token"] = get_option( "mtpc_access_token" );
     269                $mceInit['mtpc_base_url'] = $this->client->getBaseUrl();
     270            }
     271            $mceInit['mtpc_access_token'] = get_option( 'mtpc_access_token' );
    273272           
    274273            return $mceInit;
     
    278277        add_filter( 'mce_buttons',
    279278          function ( $buttons ) use ( $mce_plugin_name ) {
    280               array_push( $buttons, $mce_plugin_name . "link" );
    281               array_push( $buttons, $mce_plugin_name . "img" );
    282               array_push( $buttons, $mce_plugin_name . "collage" );
     279              $buttons[] = $mce_plugin_name . 'link';
     280              $buttons[] = $mce_plugin_name . 'img';
     281              $buttons[] = $mce_plugin_name . 'collage';
    283282             
    284283              return $buttons;
     
    291290                  'is_block_editor' )
    292291                                  && $current_screen->is_block_editor() );
    293               $api_url        = ( $this->isDocker ) ? 'http://localhost:3000'
     292              $api_url        = $this->isDocker ? 'http://localhost:3000'
    294293                : $this->getApiUrl();
    295              
    296               if ( ! $is_gutenberg ) {
    297               }
    298294             
    299295              wp_enqueue_script( 'iframeScript',
     
    317313       
    318314        add_action( 'wp_ajax_mtpc_deeplink', function () {
    319             $accessToken = get_option( "mtpc_access_token" );
    320             $userId      = get_option( "mtpc_id" );
     315            $accessToken = get_option( 'mtpc_access_token' );
     316            $userId      = get_option( 'mtpc_id' );
    321317            $newContent  = $this->client->createDeepLinks( $userId,
    322318              $_POST['links'], $accessToken );
     
    338334    private function setupHelpButton() {
    339335        add_action( 'media_buttons', function () {
    340             $this->getTemplate( "help-button" );
    341         } );
    342     }
    343     /*
    344     private function setupOptionsPage() {
    345         add_action( 'admin_init', function () {
    346             $options = get_option( 'metapic_options' );
    347             register_setting( 'metapic_options', 'metapic_options',
    348               function ( $input ) {
    349                   $options       = get_option( 'metapic_options' );
    350                   $updateActions = array_flip( [ "submit", "login" ] );
    351                   $inArray       = ( count( array_intersect_key( $_POST,
    352                       $updateActions ) ) > 0 );
    353                   $action        = ( $inArray ) ? "update" : "deactivate";
    354                   switch ( $action ) {
    355                       case "deactivate":
    356                           $this->deactivateAccount();
    357                           $this->setStatusMessage( __( "Account deactivated",
    358                             "metapic" ) );
    359                           break;
    360                       default:
    361                           $options = $this->updateOptions( $options, $input );
    362                           break;
    363                   }
    364                  
    365                   return $options;
    366               } );
    367            
    368             register_setting( 'metapic_register_options',
    369               'metapic_register_options', function ( $input ) {
    370                   $options = get_option( 'metapic_register_options' );
    371                  
    372                   $user = $this->client->register( $input["email_string"],
    373                     $input["password_string"], $input["client_string"],
    374                     get_home_url() );
    375                   if ( $user ) {
    376                       $this->activateAccount( $user["id"], $user["email"],
    377                         $user["access_token"]["access_token"] );
    378                       $this->setStatusMessage( __( "Account created",
    379                         "metapic" ) );
    380                       wp_redirect( admin_url( 'options-general.php?page=metapic_settings' ) );
    381                       die();
    382                   } else {
    383                       $this->setStatusMessage( __( "Account already exists",
    384                         "metapic" ), "error" );
    385                      
    386                   }
    387                  
    388                   return $options;
    389               } );
    390         } );
    391        
    392         add_action( 'admin_init', function () {
    393             register_setting( 'metapic_register_options',
    394               'metapic_register_options', function ( $input ) {
    395                   return $input;
    396               } );
    397         } );
    398        
    399         add_action( 'admin_menu', function () {
    400             $this->menus->register_admin_menu( $this );
    401         } );
    402     }
    403     */
    404    
    405    
    406     private function updateOptions( $options, $input ) {
    407         $options['uri_string']          = @trim( $input['uri_string'], "/" );
    408         $options['cdn_uri_string']      = @trim( $input['cdn_uri_string'],
    409           "/" );
    410         $options['user_api_uri_string'] = @trim( $input['user_api_uri_string'],
    411           "/" );
    412        
    413        
    414         $options['mtpc_deeplink_auto_default']
    415           = @(bool) $input['mtpc_deeplink_auto_default'];
    416         update_option( 'mtpc_deeplink_auto_default',
    417           @$options['mtpc_deeplink_auto_default'] );
    418        
    419         if ( ! get_option( "mtpc_active_account" )
    420              && ! get_option( "mtpc_access_token" )
    421         ) {
    422            
    423             if ( ! is_multisite() ) {
    424                 $options['email_string'] = trim( $input['email_string'] );
    425                 $password                = trim( $input['password_string'] );
    426                 try {
    427                     $user = $this->client->login( $options['email_string'],
    428                       $password );
    429                     if ( $user ) {
    430                         $this->activateAccount( $user["id"],
    431                           $options['email_string'],
    432                           $user["access_token"]["access_token"] );
    433                         $this->setStatusMessage( __( "Login successful",
    434                           "metapic" ) );
    435                     } else {
    436                         throw new Exception;
    437                     }
    438                 } catch ( Exception $e ) {
    439                     $this->deactivateAccount();
    440                     $this->setStatusMessage( __( "Invalid username or password",
    441                       "metapic" ), "error" );
    442                 }
    443             } else {
    444                 $user_email = ( isset( $_POST["mtpc_email"] ) )
    445                   ? $_POST["mtpc_email"] : wp_get_current_user()->user_email;
    446                 $wp_user    = get_user_by( "email", $user_email );
    447                 if ( $wp_user ) {
    448                     $user = $this->client->activateUser( $wp_user->user_email );
    449                    
    450                     if ( $user["access_token"] == null ) {
    451                         $this->client->createUser( array(
    452                           "email"    => $wp_user->user_email,
    453                           "username" => get_home_url()
    454                         ) );
    455                         $user
    456                           = $this->client->activateUser( $wp_user->user_email );
    457                         $this->setStatusMessage( __( "Account created",
    458                           "metapic" ) );
    459                     } else {
    460                         $this->setStatusMessage( __( "Account activated",
    461                           "metapic" ) );
    462                     }
    463                     $this->activateAccount( $user["id"], $wp_user->user_email,
    464                       $user["access_token"]["access_token"] );
    465                     add_option( 'mtpc_deeplink_auto_default',
    466                       get_site_option( 'mtpc_deeplink_auto_default' ) );
    467                    
    468                 } else {
    469                     $this->setStatusMessage( __( "User not found", "metapic" ),
    470                       "error" );
    471                 }
    472             }
    473         }
    474         if ( isset( $options["password_string"] ) ) {
    475             unset( $options["password_string"] );
    476         }
    477        
    478         return $options;
    479     }
    480    
     336            $this->getTemplate( 'help-button' );
     337        } );
     338    }
     339
    481340    private function setupLang() {
    482341        add_action( 'plugins_loaded', function () {
     
    505364        $url = false;
    506365        if ( $this->debugMode ) {
    507             $url = ( is_multisite() ) ? get_site_option( "mtpc_api_url" )
     366            $url = is_multisite() ? get_site_option( 'mtpc_api_url' )
    508367              : get_option( 'mtpc_uri_string', '' );
    509368        }
    510369       
    511         return ( $url ) ? $url : $this->api_url;
     370        return $url ? $url : $this->api_url;
    512371    }
    513372   
     
    518377            extract( $wp_query->query_vars, EXTR_SKIP );
    519378        }
    520         extract( $this->templateVars );
    521         extract( $templateVars );
    522         require( $this->plugin_dir . "/templates/{$templateName}.php" );
     379        extract( $this->templateVars, EXTR_OVERWRITE );
     380        extract( $templateVars, EXTR_OVERWRITE );
     381        require $this->plugin_dir . "/templates/{$templateName}.php";
    523382    }
    524383   
     
    547406        add_action( 'parse_request', function ( $wp ) {
    548407            if ( array_key_exists( $this->accessKey, $wp->query_vars ) ) {
    549                 $accessToken = get_option( "mtpc_access_token" );
     408                $accessToken = get_option( 'mtpc_access_token' );
    550409                if ( $this->autoRegister && ! $this->activeAccount ) {
    551410                    $user        = $this->registerCurrentUser();
    552                     $accessToken = $user["access_token"]["access_token"];
     411                    $accessToken = $user['access_token']['access_token'];
    553412                }
    554413                wp_send_json( [
    555                   "access_token" => [ "access_token" => $accessToken ],
    556                   "metapicApi"   => $this->client->getBaseUrl()
     414                    'access_token' => [ 'access_token' => $accessToken ],
     415                    'metapicApi'   => $this->client->getBaseUrl()
    557416                ] );
    558417            }
    559            
    560             return;
    561418        } );
    562419    }
     
    565422        $wp_user = wp_get_current_user();
    566423        $user    = $this->client->activateUser( $wp_user->user_email );
    567         if ( $user["access_token"] == null ) {
     424        if ( $user['access_token'] === null ) {
    568425            $this->client->createUser( array(
    569               "email"    => $wp_user->user_email,
    570               "username" => $wp_user->user_login
     426                'email'    => $wp_user->user_email,
     427                'username' => $wp_user->user_login
    571428            ) );
    572429            $user = $this->client->activateUser( $wp_user->user_email );
    573430        }
    574         $this->activateAccount( $user["id"], $wp_user->user_email,
    575           $user["access_token"]["access_token"] );
     431        $this->activate_account( $user['id'], $wp_user->user_email,
     432            $user['access_token']['access_token'] );
    576433        $this->activeAccount = true;
    577434       
     
    584441            wp_add_dashboard_widget(
    585442              'metapic-dashboard-widget',         // Widget slug.
    586               __( "Metapic", 'metapic' ),         // Title.
     443              __( 'Metapic', 'metapic' ),         // Title.
    587444              function () {
    588445                  $this->getTemplate( 'widgets/dashboard', [
    589                     "clicks" => get_option( "mtpc_clicks_by_date" ),
    590                     "month"  => get_option( "mtpc_clicks_by_month" ),
    591                     "total"  => get_option( "mtpc_clicks_total" )
     446                      'clicks' => get_option( 'mtpc_clicks_by_date' ),
     447                      'month'  => get_option( 'mtpc_clicks_by_month' ),
     448                      'total'  => get_option( 'mtpc_clicks_total' )
    592449                  ] );
    593450              }
     
    597454   
    598455    private function updateClicksForSingleSite() {
    599         $lastUpdate  = get_option( "mtpc_last_click_update" );
     456        $lastUpdate  = get_option( 'mtpc_last_click_update' );
    600457        $diffMinutes = 10;
    601458        if ( $lastUpdate ) {
     
    606463                $diffMinutes = $diff->i;
    607464            } catch ( Exception $e ) {
    608             };
    609         }
    610         if ( ( $diffMinutes >= 10 ) ) {
    611             update_site_option( "mtpc_last_click_update",
    612               date( "Y-m-d h:i:s" ) );
     465            }
     466        }
     467        if ( $diffMinutes >= 10 ) {
     468            update_site_option( 'mtpc_last_click_update',
     469              date( 'Y-m-d h:i:s' ) );
    613470            try {
    614471                $wpClicks
    615                   = $this->client->getClientClicksByDate( get_option( "mtpc_id" ),
     472                  = $this->client->getClientClicksByDate( get_option( 'mtpc_id' ),
    616473                  [
    617                     "from"              => date( 'Y-m-d',
     474                      'from'              => date( 'Y-m-d',
    618475                      strtotime( '-10 days' ) ),
    619                     "to"                => date( "Y-m-d" ),
    620                     "user_access_token" => get_option( "mtpc_access_token" )
     476                      'to'                => date( 'Y-m-d' ),
     477                      'user_access_token' => get_option( 'mtpc_access_token' )
    621478                  ] );
    622479                if ( is_array( $wpClicks ) ) {
    623                     $mtpcEmail = get_option( "mtpc_email" );
     480                    $mtpcEmail = get_option( 'mtpc_email' );
    624481                    if ( $mtpcEmail && isset( $wpClicks[ $mtpcEmail ] ) ) {
    625482                       
    626483                        $clicksToInsert
    627                           = $this->insertMissingDates( $wpClicks[ $mtpcEmail ]["day"] );
    628                         update_option( "mtpc_clicks_by_date", $clicksToInsert );
    629                         update_option( "mtpc_clicks_by_month",
    630                           isset( $wpClicks[ $mtpcEmail ]["month"] )
    631                             ? $wpClicks[ $mtpcEmail ]["month"] : 0 );
    632                         update_option( "mtpc_clicks_total",
    633                           isset( $wpClicks[ $mtpcEmail ]["total"] )
    634                             ? $wpClicks[ $mtpcEmail ]["total"] : 0 );
     484                          = $this->insertMissingDates( $wpClicks[ $mtpcEmail ]['day'] );
     485                        update_option( 'mtpc_clicks_by_date', $clicksToInsert );
     486                        update_option( 'mtpc_clicks_by_month',
     487                          isset( $wpClicks[ $mtpcEmail ]['month'] )
     488                            ? $wpClicks[ $mtpcEmail ]['month'] : 0 );
     489                        update_option( 'mtpc_clicks_total',
     490                          isset( $wpClicks[ $mtpcEmail ]['total'] )
     491                            ? $wpClicks[ $mtpcEmail ]['total'] : 0 );
    635492                    }
    636                     update_option( "mtpc_clicks", $wpClicks );
     493                    update_option( 'mtpc_clicks', $wpClicks );
    637494                }
    638495            } catch ( Exception $e ) {
     
    642499   
    643500    private function updateClicksForMultiSite() {
    644         $lastUpdate  = get_site_option( "mtpc_last_click_update" );
     501        $lastUpdate  = get_site_option( 'mtpc_last_click_update' );
    645502        $diffMinutes = 10;
    646503        if ( $lastUpdate ) {
     
    651508                $diffMinutes = $diff->i;
    652509            } catch ( Exception $e ) {
    653             };
    654         }
    655         if ( ( $diffMinutes >= 10 ) ) {
    656             update_site_option( "mtpc_last_click_update",
    657               date( "Y-m-d h:i:s" ) );
     510            }
     511        }
     512        if ( $diffMinutes >= 10 ) {
     513            update_site_option( 'mtpc_last_click_update',
     514              date( 'Y-m-d h:i:s' ) );
    658515            $orginalBloggId = get_current_blog_id();
    659516            try {
    660517                $wpClicks = $this->client->getClientClicksByDate( null, [
    661                   "from" => date( 'Y-m-d', strtotime( '-10 days' ) ),
    662                   "to"   => date( "Y-m-d" )
     518                    'from' => date( 'Y-m-d', strtotime( '-10 days' ) ),
     519                    'to'   => date( 'Y-m-d' )
    663520                ] );
    664521                $sites    = wp_get_sites();
    665522                foreach ( $sites as $site ) {
    666                     switch_to_blog( $site["blog_id"] );
    667                     $mtpcEmail = get_option( "mtpc_email" );
     523                    switch_to_blog( $site['blog_id'] );
     524                    $mtpcEmail = get_option( 'mtpc_email' );
    668525                    if ( $mtpcEmail && isset( $wpClicks[ $mtpcEmail ] ) ) {
    669526                        $clicksToInsert
    670                           = $this->insertMissingDates( $wpClicks[ $mtpcEmail ]["day"] );
    671                         update_option( "mtpc_clicks_by_date", $clicksToInsert );
    672                         update_option( "mtpc_clicks_by_month",
    673                           isset( $wpClicks[ $mtpcEmail ]["month"] )
    674                             ? $wpClicks[ $mtpcEmail ]["month"] : 0 );
    675                         update_option( "mtpc_clicks_total",
    676                           isset( $wpClicks[ $mtpcEmail ]["total"] )
    677                             ? $wpClicks[ $mtpcEmail ]["total"] : 0 );
     527                          = $this->insertMissingDates( $wpClicks[ $mtpcEmail ]['day'] );
     528                        update_option( 'mtpc_clicks_by_date', $clicksToInsert );
     529                        update_option( 'mtpc_clicks_by_month',
     530                          isset( $wpClicks[ $mtpcEmail ]['month'] )
     531                            ? $wpClicks[ $mtpcEmail ]['month'] : 0 );
     532                        update_option( 'mtpc_clicks_total',
     533                          isset( $wpClicks[ $mtpcEmail ]['total'] )
     534                            ? $wpClicks[ $mtpcEmail ]['total'] : 0 );
    678535                    }
    679536                }
    680                 update_site_option( "mtpc_clicks", $wpClicks );
     537                update_site_option( 'mtpc_clicks', $wpClicks );
    681538            } catch ( Exception $e ) {
    682539            }
     
    690547            wp_add_dashboard_widget(
    691548              'metapic-network-dashboard-widget',         // Widget slug.
    692               __( "Metapic", 'metapic' ),         // Title.
     549              __( 'Metapic', 'metapic' ),         // Title.
    693550              function () {
    694551                  $this->getTemplate( 'widgets/dashboard-network',
    695                     [ "clicks" => get_site_option( "mtpc_clicks_by_date" ) ] );
     552                    [ 'clicks' => get_site_option( 'mtpc_clicks_by_date' ) ] );
    696553              }
    697554            );
     
    707564    }
    708565   
     566    /**
     567     * @param $clicks
     568     *
     569     * @return array
     570     * @throws Exception
     571     */
    709572    private function insertMissingDates( $clicks ) {
    710573        $today      = new DateTime();
     
    713576            $clicks = [
    714577              [
    715                 "date"        => $today->format( "Y-m-d" ),
    716                 "tag_clicks"  => 0,
    717                 "link_clicks" => 0
     578                  'date'        => $today->format( 'Y-m-d' ),
     579                  'tag_clicks'  => 0,
     580                  'link_clicks' => 0
    718581              ],
    719582              [
    720                 "date"        => $tenDaysAgo->format( "Y-m-d" ),
    721                 "tag_clicks"  => 0,
    722                 "link_clicks" => 0
     583                  'date'        => $tenDaysAgo->format( 'Y-m-d' ),
     584                  'tag_clicks'  => 0,
     585                  'link_clicks' => 0
    723586              ]
    724587            ];
     
    727590        $lastClick  = end( $clicks );
    728591        reset( $clicks );
    729         $firstClickDate = new DateTime( $firstClick["date"] );
    730         $lastClickDate  = new DateTime( $lastClick["date"] );
    731         $fillIn         = [];
    732         $fillStart      = new DateTime( $firstClick["date"] );
    733         $oneDay         = new DateInterval( "P1D" );
     592
     593            $firstClickDate = new DateTime( $firstClick['date'] );
     594            $lastClickDate  = new DateTime( $lastClick['date'] );
     595            $fillIn         = [];
     596            $fillStart      = new DateTime( $firstClick['date'] );
     597            $oneDay         = new DateInterval( 'P1D' );
    734598       
    735599        foreach ( $clicks as $click ) {
    736             $clickDate = new DateTime( $click["date"] );
     600            $clickDate = new DateTime( $click['date'] );
    737601            $diff      = $fillStart->diff( $clickDate );
    738602            while ( $diff->d > 0 && $diff->invert ) {
    739603                $fillIn[]  = [
    740                   "date"        => $fillStart->format( "Y-m-d" ),
    741                   "tag_clicks"  => 0,
    742                   "link_clicks" => 0
     604                    'date'        => $fillStart->format( 'Y-m-d' ),
     605                    'tag_clicks'  => 0,
     606                    'link_clicks' => 0
    743607                ];
    744608                $fillStart = $fillStart->sub( $oneDay );
     
    746610            }
    747611            $fillIn[]  = $click;
    748             $fillStart = ( new DateTime( $click["date"] ) )->sub( $oneDay );
     612            $fillStart = ( new DateTime( $click['date'] ) )->sub( $oneDay );
    749613        }
    750614        $clicks = $fillIn;
    751615       
    752616        $tempClicks = [];
    753         $diff       = $today->diff( $firstClickDate, false );
     617        $diff       = $today->diff( $firstClickDate );
    754618        while ( $diff->d > 0 && $diff->invert ) {
    755619            $tempClicks[] = array_merge( $firstClick, [
    756               "date"        => $today->format( "Y-m-d" ),
    757               "tag_clicks"  => 0,
    758               "link_clicks" => 0
     620                'date'        => $today->format( 'Y-m-d' ),
     621                'tag_clicks'  => 0,
     622                'link_clicks' => 0
    759623            ] );
    760624            $today        = $today->sub( $oneDay );
    761             $diff         = $today->diff( $firstClickDate, false );
     625            $diff         = $today->diff( $firstClickDate );
    762626        }
    763627        $clicks = array_merge( $tempClicks, $clicks );
    764628       
    765629        $tempClicks = [];
    766         $tenDayDiff = $tenDaysAgo->diff( $lastClickDate, false );
     630        $tenDayDiff = $tenDaysAgo->diff( $lastClickDate );
    767631        while ( $tenDayDiff->d > 0 && ! $tenDayDiff->invert ) {
    768632            $tempClicks[] = array_merge( $firstClick, [
    769               "date"        => $tenDaysAgo->format( "Y-m-d" ),
    770               "tag_clicks"  => 0,
    771               "link_clicks" => 0
     633                'date'        => $tenDaysAgo->format( 'Y-m-d' ),
     634                'tag_clicks'  => 0,
     635                'link_clicks' => 0
    772636            ] );
    773637            $tenDaysAgo   = $tenDaysAgo->add( $oneDay );
    774             $tenDayDiff   = $tenDaysAgo->diff( $lastClickDate, false );
     638            $tenDayDiff   = $tenDaysAgo->diff( $lastClickDate );
    775639        }
    776640        $clicks = array_merge( $clicks, array_reverse( $tempClicks ) );
    777641       
    778642        return $clicks;
    779     }
    780    
    781 
    782    
    783     private function deactivateAccount() {
    784         delete_option( "mtpc_active_account" );
    785         delete_option( "mtpc_access_token" );
    786         delete_option( "mtpc_email" );
    787         delete_option( "mtpc_id" );
    788         delete_option( 'mtpc_deeplink_auto_default' );
    789643    }
    790644   
     
    795649       
    796650        add_action( 'save_post', function ( $postId ) {
    797             if ( isset( $_POST["mtpc_deeplink_auto"] ) ) {
    798                 update_post_meta( $postId, "mtpc_deeplink_auto",
    799                   (int) $_POST["mtpc_deeplink_auto"] );
    800             }
    801         } );
    802        
    803         /*
    804         add_filter('wp_insert_post_data', function ($filtered_data, $raw_data) {
    805             $deepLinkContent = (isset($raw_data["mtpc_deeplink_auto"])) ? (bool)$raw_data["mtpc_deeplink_auto"] : false;
    806             if ($deepLinkContent) {
    807                 if (!$this->hasActiveAccount() && $this->autoRegister) {
    808                     $this->registerCurrentUser();
    809                 }
    810                 $userId = get_option("mtpc_id");
    811                 $accessToken = get_option("mtpc_access_token");
    812                 $newContent = $this->client->deepLinkBlogPost($userId, $filtered_data['post_content'], $accessToken);
    813 
    814                 if (is_array($newContent) && isset($newContent["newHtml"]) && $newContent["isUpdated"]) {
    815                     $filtered_data['post_content'] = $newContent["newHtml"];
    816                 }
    817             }
    818             return $filtered_data;
    819         }, 10, 2);
    820         */
    821     }
    822    
    823     private function optionExists( $optionName ) {
    824         return ( get_option( $optionName ) === false );
    825     }
    826    
    827     private function siteOptionExists( $optionName ) {
    828         return ( get_site_option( $optionName ) === false );
    829     }
    830    
    831     private function isEditPage( $new_edit = null ) {
    832         global $pagenow;
    833        
    834         if ( ! is_admin() ) {
    835             return false;
    836         }
    837        
    838         if ( $new_edit == "edit" ) {
    839             return in_array( $pagenow, array( 'post.php', ) );
    840         } elseif ( $new_edit == "new" ) {
    841             return in_array( $pagenow, array( 'post-new.php' ) );
    842         } else {
    843             return in_array( $pagenow, array( 'post.php', 'post-new.php' ) );
    844         }
     651            if ( isset( $_POST['mtpc_deeplink_auto'] ) ) {
     652                update_post_meta( $postId, 'mtpc_deeplink_auto',
     653                  (int) $_POST['mtpc_deeplink_auto'] );
     654            }
     655        } );
    845656    }
    846657}
  • metapic/trunk/classes/WP_MTPC_Settings.php

    r2065457 r2065460  
    9090                $return_url = remove_query_arg( 'mtpc-account-activated',
    9191                    $return_url );
    92                
    93                 if ( isset( $_POST['deactivate'] ) ) {
     92
     93                if ( isset( $_POST['deactivate'] )
     94                     || isset( $_POST['logout'] )
     95                ) {
     96
    9497                    $this->deactivate_account();
    95                     $return_url = add_query_arg( 'mtpc-account-deactivated', 1,
    96                         admin_url( 'admin.php?page=metapic' ) );
     98                   
     99                    $query_arg = isset( $_POST['deactivate'] )
     100                        ? 'mtpc-account-deactivated'
     101                        : 'mtpc-user-logout';
     102                   
     103                    $url = isset( $_POST['deactivate'] )
     104                        ? 'admin.php?page=metapic'
     105                        : 'admin.php?page=metapic-login';
     106                   
     107                    $return_url = add_query_arg( $query_arg, 1,
     108                        admin_url( $url ) );
     109                   
    97110                } else {
    98111                    update_option( 'mtpc_deeplink_auto_default',
     
    120133    }
    121134   
    122     public function handle_mtpc_user_new( WP_MTPC $mtpc ) {
     135    public function handle_mtpc_user_new() {
    123136        add_action( 'admin_post_mtpc_user_new',
    124             function () use ( $mtpc ) {
     137            function () {
    125138                check_admin_referer( 'mtpc_user_new', 'mtpc' );
    126139               
Note: See TracChangeset for help on using the changeset viewer.