Ga_Admin_Controller.php
118 lines
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * Manages actions in the admin area. |
| 5 | * |
| 6 | * Created by PhpStorm. |
| 7 | * User: mdn |
| 8 | * Date: 2017-01-25 |
| 9 | * Time: 09:50 |
| 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 | if ( Ga_Helper::are_features_enabled() ) { |
| 20 | header( 'Location:' . Ga_Admin::api_client()->create_auth_url() ); |
| 21 | } else { |
| 22 | wp_die( Ga_Helper::ga_oauth_notice( __( 'Please accept the terms to use this feature' ) ) ); |
| 23 | } |
| 24 | } |
| 25 | |
| 26 | /** |
| 27 | * Handle Sharethis invite action |
| 28 | */ |
| 29 | public static function ga_action_sharethis_invite() { |
| 30 | |
| 31 | if ( self::verify_nonce( self::ACTION_SHARETHIS_INVITE ) ) { |
| 32 | $email = !empty( $_POST[ 'sharethis_invite_email' ] ) ? $_POST[ 'sharethis_invite_email' ] : null; |
| 33 | $response = null; |
| 34 | if ( !empty( $email ) ) { |
| 35 | $data = array( |
| 36 | 'id' => get_option( Ga_Admin::GA_SHARETHIS_PROPERTY_ID ), |
| 37 | 'secret' => get_option( Ga_Admin::GA_SHARETHIS_PROPERTY_SECRET ), |
| 38 | 'product' => 'viral-notifications', |
| 39 | 'role' => 'admin', // array_shift(Ga_Helper::get_user_roles()) |
| 40 | 'email' => $email |
| 41 | ); |
| 42 | |
| 43 | $response = Ga_Admin::api_client( Ga_Admin::GA_SHARETHIS_API_ALIAS )->call( 'ga_api_sharethis_user_invite', array( $data ) ); |
| 44 | $errors = Ga_Admin::api_client( Ga_Admin::GA_SHARETHIS_API_ALIAS )->get_errors(); |
| 45 | |
| 46 | if ( !empty( $errors ) ) { |
| 47 | $msg = ''; |
| 48 | foreach ( $errors as $error ) { |
| 49 | $msg .= $error[ 'message' ]; |
| 50 | } |
| 51 | $msg = Ga_Helper::create_url_msg( $msg, Ga_Admin::NOTICE_ERROR ); |
| 52 | } else { |
| 53 | $msg = Ga_Helper::create_url_msg( _( 'An invite was sent to this email' ), Ga_Admin::NOTICE_SUCCESS ); |
| 54 | } |
| 55 | } |
| 56 | } else { |
| 57 | $msg = Ga_Helper::create_url_msg( _( 'Invalid request.' ), Ga_Admin::NOTICE_ERROR ); |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | /** |
| 62 | * Sets accept terms option to TRUE. |
| 63 | */ |
| 64 | public static function ga_action_update_terms() { |
| 65 | update_option( Ga_Admin::GA_SHARETHIS_TERMS_OPTION_NAME, true ); |
| 66 | |
| 67 | wp_redirect( admin_url( Ga_Helper::GA_SETTINGS_PAGE_URL ) ); |
| 68 | } |
| 69 | |
| 70 | /** |
| 71 | * Enables all features option. |
| 72 | */ |
| 73 | public static function ga_action_enable_all_features() { |
| 74 | Ga_Helper::update_option( Ga_Admin::GA_DISABLE_ALL_FEATURES, false ); |
| 75 | |
| 76 | $url = !empty( $_GET[ 'page' ] ) ? Ga_Helper::create_url( admin_url( 'admin.php' ), array( 'page' => $_GET[ 'page' ] ) ) : admin_url( Ga_Helper::create_url( Ga_Helper::GA_SETTINGS_PAGE_URL ) ); |
| 77 | |
| 78 | wp_redirect( $url ); |
| 79 | } |
| 80 | |
| 81 | /** |
| 82 | * Disables all features option. |
| 83 | */ |
| 84 | public static function ga_action_disable_all_features() { |
| 85 | Ga_Helper::update_option( Ga_Admin::GA_DISABLE_ALL_FEATURES, true ); |
| 86 | |
| 87 | $url = !empty( $_GET[ 'page' ] ) ? Ga_Helper::create_url( admin_url( 'admin.php' ), array( 'page' => $_GET[ 'page' ] ) ) : admin_url( Ga_Helper::create_url( Ga_Helper::GA_SETTINGS_PAGE_URL ) ); |
| 88 | |
| 89 | wp_redirect( $url ); |
| 90 | } |
| 91 | |
| 92 | public static function validate_ajax_data_change_post( $post ) { |
| 93 | $error = 0; |
| 94 | |
| 95 | if ( self::verify_nonce( 'ga_ajax_data_change' ) ) { |
| 96 | if ( !empty( $post[ 'date_range' ] ) ) { |
| 97 | if ( !is_string( $post[ 'date_range' ] ) ) { |
| 98 | $error ++; |
| 99 | } |
| 100 | } else { |
| 101 | $error ++; |
| 102 | } |
| 103 | |
| 104 | if ( !empty( $post[ 'metric' ] ) ) { |
| 105 | if ( !is_string( $post[ 'metric' ] ) ) { |
| 106 | $error ++; |
| 107 | } |
| 108 | } else { |
| 109 | $error ++; |
| 110 | } |
| 111 | } else { |
| 112 | $error ++; |
| 113 | } |
| 114 | |
| 115 | return $error == 0; |
| 116 | } |
| 117 | } |
| 118 |