Changeset 2065460
- Timestamp:
- 04/09/2019 02:47:35 AM (7 years ago)
- Location:
- metapic/trunk/classes
- Files:
-
- 2 edited
-
WP_MTPC.php (modified) (29 diffs)
-
WP_MTPC_Settings.php (modified) (2 diffs)
Legend:
- Unmodified
- Added
- Removed
-
metapic/trunk/classes/WP_MTPC.php
r2065457 r2065460 6 6 use WP_MTPC_Utilities; 7 7 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'; 11 11 private $plugin_dir; 12 12 private $plugin_url; … … 28 28 29 29 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; 34 34 private $autoRegister = false; 35 private $activeAccount = false;35 private $activeAccount; 36 36 private $forceSSL = true; 37 37 38 38 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' ); 44 44 45 45 $this->plugin_dir = $plugin_dir; … … 48 48 49 49 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 ) { 56 56 $this->api_url = MTPC_API_URL; 57 57 } 58 58 59 59 $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' ), '/' ) . '/?' 64 64 . $this->accessKey; 65 65 … … 92 92 'status' => 'error' 93 93 ], 94 95 94 [ 96 95 'type' => 'mtpc-user-logout', … … 134 133 if ( is_multisite() ) { 135 134 $this->autoRegister 136 = (bool) get_site_option( "mtpc_registration_auto");135 = (bool) get_site_option( 'mtpc_registration_auto' ); 137 136 } 138 137 $this->activeAccount = $this->has_active_account(); … … 140 139 $this->settings->handle_admin_ms_settings( $this->getApiUrl() ); 141 140 $this->settings->handle_mtpc_user_my_account($this->debugMode); 142 $this->settings->handle_mtpc_user_new( $this);141 $this->settings->handle_mtpc_user_new( ); 143 142 $this->settings->handle_mtpc_user_reactivate(); 144 143 … … 158 157 add_filter( 'wp_kses_allowed_html', function ( $tags, $context ) { 159 158 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; 163 162 } 164 163 … … 178 177 private function setupFrontendJsOptions() { 179 178 $jsHandle = 'mtpc_frontend_js'; 180 add_action( "wp_head", function () use ( $jsHandle ) {179 add_action( 'wp_head', function () use ( $jsHandle ) { 181 180 if ( $this->debugMode ) { 182 181 … … 225 224 }, 10 ); 226 225 227 add_action( "wp_footer", function () {228 $this->getTemplate( "metapic-load");226 add_action( 'wp_footer', function () { 227 $this->getTemplate( 'metapic-load' ); 229 228 } ); 230 229 } 231 230 232 231 private function setupBackendJsOptions() { 233 $baseUrl = ( $this->isDocker ) ? "http://localhost:3000"232 $baseUrl = $this->isDocker ? 'http://localhost:3000' 234 233 : $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'; 237 236 238 237 // Declare script for new button … … 263 262 264 263 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; 267 266 if ( $this->isDocker ) { 268 $mceInit[ "mtpc_base_url"] = "http://localhost:3000";267 $mceInit['mtpc_base_url'] = 'http://localhost:3000'; 269 268 } 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' ); 273 272 274 273 return $mceInit; … … 278 277 add_filter( 'mce_buttons', 279 278 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'; 283 282 284 283 return $buttons; … … 291 290 'is_block_editor' ) 292 291 && $current_screen->is_block_editor() ); 293 $api_url = ( $this->isDocker )? 'http://localhost:3000'292 $api_url = $this->isDocker ? 'http://localhost:3000' 294 293 : $this->getApiUrl(); 295 296 if ( ! $is_gutenberg ) {297 }298 294 299 295 wp_enqueue_script( 'iframeScript', … … 317 313 318 314 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' ); 321 317 $newContent = $this->client->createDeepLinks( $userId, 322 318 $_POST['links'], $accessToken ); … … 338 334 private function setupHelpButton() { 339 335 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 481 340 private function setupLang() { 482 341 add_action( 'plugins_loaded', function () { … … 505 364 $url = false; 506 365 if ( $this->debugMode ) { 507 $url = ( is_multisite() ) ? get_site_option( "mtpc_api_url")366 $url = is_multisite() ? get_site_option( 'mtpc_api_url' ) 508 367 : get_option( 'mtpc_uri_string', '' ); 509 368 } 510 369 511 return ( $url )? $url : $this->api_url;370 return $url ? $url : $this->api_url; 512 371 } 513 372 … … 518 377 extract( $wp_query->query_vars, EXTR_SKIP ); 519 378 } 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"; 523 382 } 524 383 … … 547 406 add_action( 'parse_request', function ( $wp ) { 548 407 if ( array_key_exists( $this->accessKey, $wp->query_vars ) ) { 549 $accessToken = get_option( "mtpc_access_token");408 $accessToken = get_option( 'mtpc_access_token' ); 550 409 if ( $this->autoRegister && ! $this->activeAccount ) { 551 410 $user = $this->registerCurrentUser(); 552 $accessToken = $user[ "access_token"]["access_token"];411 $accessToken = $user['access_token']['access_token']; 553 412 } 554 413 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() 557 416 ] ); 558 417 } 559 560 return;561 418 } ); 562 419 } … … 565 422 $wp_user = wp_get_current_user(); 566 423 $user = $this->client->activateUser( $wp_user->user_email ); 567 if ( $user[ "access_token"]== null ) {424 if ( $user['access_token'] === null ) { 568 425 $this->client->createUser( array( 569 "email"=> $wp_user->user_email,570 "username"=> $wp_user->user_login426 'email' => $wp_user->user_email, 427 'username' => $wp_user->user_login 571 428 ) ); 572 429 $user = $this->client->activateUser( $wp_user->user_email ); 573 430 } 574 $this->activate Account( $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'] ); 576 433 $this->activeAccount = true; 577 434 … … 584 441 wp_add_dashboard_widget( 585 442 'metapic-dashboard-widget', // Widget slug. 586 __( "Metapic", 'metapic' ), // Title.443 __( 'Metapic', 'metapic' ), // Title. 587 444 function () { 588 445 $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' ) 592 449 ] ); 593 450 } … … 597 454 598 455 private function updateClicksForSingleSite() { 599 $lastUpdate = get_option( "mtpc_last_click_update");456 $lastUpdate = get_option( 'mtpc_last_click_update' ); 600 457 $diffMinutes = 10; 601 458 if ( $lastUpdate ) { … … 606 463 $diffMinutes = $diff->i; 607 464 } 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' ) ); 613 470 try { 614 471 $wpClicks 615 = $this->client->getClientClicksByDate( get_option( "mtpc_id"),472 = $this->client->getClientClicksByDate( get_option( 'mtpc_id' ), 616 473 [ 617 "from"=> date( 'Y-m-d',474 'from' => date( 'Y-m-d', 618 475 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' ) 621 478 ] ); 622 479 if ( is_array( $wpClicks ) ) { 623 $mtpcEmail = get_option( "mtpc_email");480 $mtpcEmail = get_option( 'mtpc_email' ); 624 481 if ( $mtpcEmail && isset( $wpClicks[ $mtpcEmail ] ) ) { 625 482 626 483 $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 ); 635 492 } 636 update_option( "mtpc_clicks", $wpClicks );493 update_option( 'mtpc_clicks', $wpClicks ); 637 494 } 638 495 } catch ( Exception $e ) { … … 642 499 643 500 private function updateClicksForMultiSite() { 644 $lastUpdate = get_site_option( "mtpc_last_click_update");501 $lastUpdate = get_site_option( 'mtpc_last_click_update' ); 645 502 $diffMinutes = 10; 646 503 if ( $lastUpdate ) { … … 651 508 $diffMinutes = $diff->i; 652 509 } 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' ) ); 658 515 $orginalBloggId = get_current_blog_id(); 659 516 try { 660 517 $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' ) 663 520 ] ); 664 521 $sites = wp_get_sites(); 665 522 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' ); 668 525 if ( $mtpcEmail && isset( $wpClicks[ $mtpcEmail ] ) ) { 669 526 $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 ); 678 535 } 679 536 } 680 update_site_option( "mtpc_clicks", $wpClicks );537 update_site_option( 'mtpc_clicks', $wpClicks ); 681 538 } catch ( Exception $e ) { 682 539 } … … 690 547 wp_add_dashboard_widget( 691 548 'metapic-network-dashboard-widget', // Widget slug. 692 __( "Metapic", 'metapic' ), // Title.549 __( 'Metapic', 'metapic' ), // Title. 693 550 function () { 694 551 $this->getTemplate( 'widgets/dashboard-network', 695 [ "clicks" => get_site_option( "mtpc_clicks_by_date") ] );552 [ 'clicks' => get_site_option( 'mtpc_clicks_by_date' ) ] ); 696 553 } 697 554 ); … … 707 564 } 708 565 566 /** 567 * @param $clicks 568 * 569 * @return array 570 * @throws Exception 571 */ 709 572 private function insertMissingDates( $clicks ) { 710 573 $today = new DateTime(); … … 713 576 $clicks = [ 714 577 [ 715 "date" => $today->format( "Y-m-d"),716 "tag_clicks"=> 0,717 "link_clicks"=> 0578 'date' => $today->format( 'Y-m-d' ), 579 'tag_clicks' => 0, 580 'link_clicks' => 0 718 581 ], 719 582 [ 720 "date" => $tenDaysAgo->format( "Y-m-d"),721 "tag_clicks"=> 0,722 "link_clicks"=> 0583 'date' => $tenDaysAgo->format( 'Y-m-d' ), 584 'tag_clicks' => 0, 585 'link_clicks' => 0 723 586 ] 724 587 ]; … … 727 590 $lastClick = end( $clicks ); 728 591 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' ); 734 598 735 599 foreach ( $clicks as $click ) { 736 $clickDate = new DateTime( $click[ "date"] );600 $clickDate = new DateTime( $click['date'] ); 737 601 $diff = $fillStart->diff( $clickDate ); 738 602 while ( $diff->d > 0 && $diff->invert ) { 739 603 $fillIn[] = [ 740 "date" => $fillStart->format( "Y-m-d"),741 "tag_clicks"=> 0,742 "link_clicks"=> 0604 'date' => $fillStart->format( 'Y-m-d' ), 605 'tag_clicks' => 0, 606 'link_clicks' => 0 743 607 ]; 744 608 $fillStart = $fillStart->sub( $oneDay ); … … 746 610 } 747 611 $fillIn[] = $click; 748 $fillStart = ( new DateTime( $click[ "date"] ) )->sub( $oneDay );612 $fillStart = ( new DateTime( $click['date'] ) )->sub( $oneDay ); 749 613 } 750 614 $clicks = $fillIn; 751 615 752 616 $tempClicks = []; 753 $diff = $today->diff( $firstClickDate , false);617 $diff = $today->diff( $firstClickDate ); 754 618 while ( $diff->d > 0 && $diff->invert ) { 755 619 $tempClicks[] = array_merge( $firstClick, [ 756 "date" => $today->format( "Y-m-d"),757 "tag_clicks"=> 0,758 "link_clicks"=> 0620 'date' => $today->format( 'Y-m-d' ), 621 'tag_clicks' => 0, 622 'link_clicks' => 0 759 623 ] ); 760 624 $today = $today->sub( $oneDay ); 761 $diff = $today->diff( $firstClickDate , false);625 $diff = $today->diff( $firstClickDate ); 762 626 } 763 627 $clicks = array_merge( $tempClicks, $clicks ); 764 628 765 629 $tempClicks = []; 766 $tenDayDiff = $tenDaysAgo->diff( $lastClickDate , false);630 $tenDayDiff = $tenDaysAgo->diff( $lastClickDate ); 767 631 while ( $tenDayDiff->d > 0 && ! $tenDayDiff->invert ) { 768 632 $tempClicks[] = array_merge( $firstClick, [ 769 "date" => $tenDaysAgo->format( "Y-m-d"),770 "tag_clicks"=> 0,771 "link_clicks"=> 0633 'date' => $tenDaysAgo->format( 'Y-m-d' ), 634 'tag_clicks' => 0, 635 'link_clicks' => 0 772 636 ] ); 773 637 $tenDaysAgo = $tenDaysAgo->add( $oneDay ); 774 $tenDayDiff = $tenDaysAgo->diff( $lastClickDate , false);638 $tenDayDiff = $tenDaysAgo->diff( $lastClickDate ); 775 639 } 776 640 $clicks = array_merge( $clicks, array_reverse( $tempClicks ) ); 777 641 778 642 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' );789 643 } 790 644 … … 795 649 796 650 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 } ); 845 656 } 846 657 } -
metapic/trunk/classes/WP_MTPC_Settings.php
r2065457 r2065460 90 90 $return_url = remove_query_arg( 'mtpc-account-activated', 91 91 $return_url ); 92 93 if ( isset( $_POST['deactivate'] ) ) { 92 93 if ( isset( $_POST['deactivate'] ) 94 || isset( $_POST['logout'] ) 95 ) { 96 94 97 $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 97 110 } else { 98 111 update_option( 'mtpc_deeplink_auto_default', … … 120 133 } 121 134 122 public function handle_mtpc_user_new( WP_MTPC $mtpc) {135 public function handle_mtpc_user_new() { 123 136 add_action( 'admin_post_mtpc_user_new', 124 function () use ( $mtpc ){137 function () { 125 138 check_admin_referer( 'mtpc_user_new', 'mtpc' ); 126 139
Note: See TracChangeset
for help on using the changeset viewer.