class-ga-admin-controller.php
140 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Google Analytics admin controller. |
| 4 | * |
| 5 | * @package GoogleAnalytics |
| 6 | */ |
| 7 | |
| 8 | /** |
| 9 | * Admin controller. |
| 10 | */ |
| 11 | class Ga_Admin_Controller extends Ga_Controller_Core { |
| 12 | |
| 13 | const ACTION_SHARETHIS_INVITE = 'ga_action_sharethis_invite'; |
| 14 | |
| 15 | /** |
| 16 | * Redirects to Google oauth authentication endpoint. |
| 17 | */ |
| 18 | public static function ga_action_auth() { |
| 19 | header( 'Location:' . Ga_Admin::api_client()->create_auth_url() ); |
| 20 | } |
| 21 | |
| 22 | /** |
| 23 | * Handle Sharethis invite action |
| 24 | */ |
| 25 | public static function ga_action_sharethis_invite() { |
| 26 | if ( true === self::verify_nonce( self::ACTION_SHARETHIS_INVITE ) ) { |
| 27 | // Validate email. |
| 28 | $email = filter_input( INPUT_POST, 'sharethis_invite_email', FILTER_SANITIZE_EMAIL ); |
| 29 | |
| 30 | if ( false === empty( $email ) ) { |
| 31 | $data = array( |
| 32 | 'id' => get_option( Ga_Admin::GA_SHARETHIS_PROPERTY_ID ), |
| 33 | 'secret' => get_option( Ga_Admin::GA_SHARETHIS_PROPERTY_SECRET ), |
| 34 | 'product' => 'viral-notifications', |
| 35 | 'role' => 'admin', |
| 36 | 'email' => $email, |
| 37 | ); |
| 38 | |
| 39 | Ga_Admin::api_client( Ga_Admin::GA_SHARETHIS_API_ALIAS ) |
| 40 | ->call( 'ga_api_sharethis_user_invite', array( $data ) ); |
| 41 | |
| 42 | $errors = Ga_Admin::api_client( Ga_Admin::GA_SHARETHIS_API_ALIAS )->get_errors(); |
| 43 | |
| 44 | if ( false === empty( $errors ) ) { |
| 45 | $msg = ''; |
| 46 | foreach ( $errors as $error ) { |
| 47 | $msg .= $error['message']; |
| 48 | } |
| 49 | $msg = Ga_Helper::create_url_msg( $msg, Ga_Admin::NOTICE_ERROR ); |
| 50 | } else { |
| 51 | $msg = Ga_Helper::create_url_msg( |
| 52 | __( 'An invite was sent to this email' ), |
| 53 | Ga_Admin::NOTICE_SUCCESS |
| 54 | ); |
| 55 | } |
| 56 | } |
| 57 | } else { |
| 58 | $msg = Ga_Helper::create_url_msg( |
| 59 | __( 'Invalid request.' ), |
| 60 | Ga_Admin::NOTICE_ERROR |
| 61 | ); |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | /** |
| 66 | * Sets accept terms option to TRUE. |
| 67 | */ |
| 68 | public static function ga_action_update_terms() { |
| 69 | update_option( Ga_Admin::GA_SHARETHIS_TERMS_OPTION_NAME, true ); |
| 70 | |
| 71 | wp_safe_redirect( admin_url( Ga_Helper::GA_SETTINGS_PAGE_URL ) ); |
| 72 | } |
| 73 | |
| 74 | /** |
| 75 | * Enables all features option. |
| 76 | */ |
| 77 | public static function ga_action_enable_all_features() { |
| 78 | Ga_Helper::update_option( Ga_Admin::GA_DISABLE_ALL_FEATURES, false ); |
| 79 | |
| 80 | $page = filter_input( INPUT_GET, 'page', FILTER_SANITIZE_STRING ); |
| 81 | |
| 82 | if ( false === empty( $page ) ) { |
| 83 | $url = Ga_Helper::create_url( admin_url( 'admin.php' ), compact( 'page' ) ); |
| 84 | } else { |
| 85 | $url = admin_url( Ga_Helper::create_url( Ga_Helper::GA_SETTINGS_PAGE_URL ) ); |
| 86 | } |
| 87 | |
| 88 | wp_safe_redirect( $url ); |
| 89 | } |
| 90 | |
| 91 | /** |
| 92 | * Disables all features option. |
| 93 | */ |
| 94 | public static function ga_action_disable_all_features() { |
| 95 | Ga_Helper::update_option( Ga_Admin::GA_DISABLE_ALL_FEATURES, true ); |
| 96 | |
| 97 | $page = filter_input( INPUT_GET, 'page', FILTER_SANITIZE_STRING ); |
| 98 | |
| 99 | $url = false === empty( $page ) ? |
| 100 | Ga_Helper::create_url( admin_url( 'admin.php' ), compact( 'page' ) ) : |
| 101 | admin_url( Ga_Helper::create_url( Ga_Helper::GA_SETTINGS_PAGE_URL ) ); |
| 102 | |
| 103 | wp_safe_redirect( $url ); |
| 104 | } |
| 105 | |
| 106 | /** |
| 107 | * Validate data change post ajax call. |
| 108 | * |
| 109 | * @return bool |
| 110 | */ |
| 111 | public static function validate_ajax_data_change_post() { |
| 112 | $error = 0; |
| 113 | |
| 114 | $date_range = filter_input( INPUT_POST, 'date_range', FILTER_SANITIZE_STRING ); |
| 115 | $metric = filter_input( INPUT_POST, 'metric', FILTER_SANITIZE_STRING ); |
| 116 | |
| 117 | if ( true === self::verify_nonce( 'ga_ajax_data_change' ) ) { |
| 118 | if ( false === empty( $date_range ) ) { |
| 119 | if ( false === is_string( $date_range ) ) { |
| 120 | $error ++; |
| 121 | } |
| 122 | } else { |
| 123 | $error ++; |
| 124 | } |
| 125 | |
| 126 | if ( false === empty( $metric ) ) { |
| 127 | if ( false === is_string( $metric ) ) { |
| 128 | $error ++; |
| 129 | } |
| 130 | } else { |
| 131 | $error ++; |
| 132 | } |
| 133 | } else { |
| 134 | $error ++; |
| 135 | } |
| 136 | |
| 137 | return 0 === $error; |
| 138 | } |
| 139 | } |
| 140 |