Total Complexity | 216 |
Total Lines | 1610 |
Duplicated Lines | 0 % |
Changes | 2 | ||
Bugs | 0 | Features | 0 |
Complex classes like MonsterInsights_Rest_Routes often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use MonsterInsights_Rest_Routes, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
11 | class MonsterInsights_Rest_Routes { |
||
12 | |||
13 | /** |
||
14 | * MonsterInsights_Rest_Routes constructor. |
||
15 | */ |
||
16 | public function __construct() { |
||
56 | ) ); |
||
57 | } |
||
58 | |||
59 | /** |
||
60 | * Ajax handler for grabbing the license |
||
61 | */ |
||
62 | public function get_license() { |
||
63 | |||
64 | check_ajax_referer( 'mi-admin-nonce', 'nonce' ); |
||
65 | |||
66 | if ( ! current_user_can( 'monsterinsights_view_dashboard' ) || ! monsterinsights_is_pro_version() ) { |
||
67 | return; |
||
68 | } |
||
69 | |||
70 | $site_license = array( |
||
71 | 'key' => MonsterInsights()->license->get_site_license_key(), |
||
72 | 'type' => MonsterInsights()->license->get_site_license_type(), |
||
73 | 'is_disabled' => MonsterInsights()->license->site_license_disabled(), |
||
74 | 'is_expired' => MonsterInsights()->license->site_license_expired(), |
||
75 | 'expiry_date' => MonsterInsights()->license->get_license_expiry_date(), |
||
76 | 'is_invalid' => MonsterInsights()->license->site_license_invalid(), |
||
77 | 'is_agency' => MonsterInsights()->license->site_is_agency(), |
||
78 | ); |
||
79 | $network_license = array( |
||
80 | 'key' => MonsterInsights()->license->get_network_license_key(), |
||
81 | 'type' => MonsterInsights()->license->get_network_license_type(), |
||
82 | 'is_disabled' => MonsterInsights()->license->network_license_disabled(), |
||
83 | 'is_expired' => MonsterInsights()->license->network_license_expired(), |
||
84 | 'expiry_date' => MonsterInsights()->license->get_license_expiry_date(), |
||
85 | 'is_invalid' => MonsterInsights()->license->network_license_disabled(), |
||
86 | 'is_agency' => MonsterInsights()->license->network_is_agency(), |
||
87 | ); |
||
88 | |||
89 | wp_send_json( array( |
||
90 | 'site' => $site_license, |
||
91 | 'network' => $network_license, |
||
92 | ) ); |
||
93 | |||
94 | } |
||
95 | |||
96 | /** |
||
97 | * Ajax handler for grabbing the current authenticated profile. |
||
98 | */ |
||
99 | public function get_profile() { |
||
100 | check_ajax_referer( 'mi-admin-nonce', 'nonce' ); |
||
101 | |||
102 | if ( ! current_user_can( 'monsterinsights_save_settings' ) ) { |
||
103 | return; |
||
104 | } |
||
105 | |||
106 | $auth = MonsterInsights()->auth; |
||
107 | |||
108 | wp_send_json( array( |
||
109 | 'v4' => $auth->get_v4_id(), |
||
110 | 'viewname' => $auth->get_viewname(), |
||
111 | 'manual_v4' => $auth->get_manual_v4_id(), |
||
112 | 'measurement_protocol_secret' => $auth->get_measurement_protocol_secret(), |
||
113 | 'network_v4' => $auth->get_network_v4_id(), |
||
114 | 'network_viewname' => $auth->get_network_viewname(), |
||
115 | 'network_manual_v4' => $auth->get_network_manual_v4_id(), |
||
116 | 'network_measurement_protocol_secret' => $auth->get_network_measurement_protocol_secret(), |
||
117 | ) ); |
||
118 | |||
119 | } |
||
120 | |||
121 | /** |
||
122 | * Ajax handler for grabbing the settings. |
||
123 | */ |
||
124 | public function get_settings() { |
||
125 | |||
126 | check_ajax_referer( 'mi-admin-nonce', 'nonce' ); |
||
127 | |||
128 | if ( ! current_user_can( 'monsterinsights_view_dashboard' ) ) { |
||
129 | return; |
||
130 | } |
||
131 | |||
132 | $options = monsterinsights_get_options(); |
||
133 | |||
134 | // Array fields are needed even if empty. |
||
135 | $array_fields = array( 'view_reports', 'save_settings', 'ignore_users' ); |
||
136 | foreach ( $array_fields as $array_field ) { |
||
137 | if ( ! isset( $options[ $array_field ] ) ) { |
||
138 | $options[ $array_field ] = array(); |
||
139 | } |
||
140 | } |
||
141 | |||
142 | //add email summaries options |
||
143 | if ( monsterinsights_is_pro_version() ) { |
||
144 | $default_email = array( |
||
145 | 'email' => get_option( 'admin_email' ), |
||
146 | ); |
||
147 | |||
148 | if ( ! isset( $options['email_summaries'] ) ) { |
||
149 | $options['email_summaries'] = 'on'; |
||
150 | } |
||
151 | |||
152 | if ( ! isset( $options['summaries_email_addresses'] ) ) { |
||
153 | $options['summaries_email_addresses'] = array( |
||
154 | $default_email, |
||
155 | ); |
||
156 | } |
||
157 | |||
158 | if ( ! isset( $options['summaries_html_template'] ) ) { |
||
159 | $options['summaries_html_template'] = 'yes'; |
||
160 | } |
||
161 | |||
162 | |||
163 | if ( ! isset( $options['summaries_carbon_copy'] ) ) { |
||
164 | $options['summaries_carbon_copy'] = 'no'; |
||
165 | } |
||
166 | |||
167 | |||
168 | if ( ! isset( $options['summaries_header_image'] ) ) { |
||
169 | $options['summaries_header_image'] = ''; |
||
170 | } |
||
171 | |||
172 | if ( ! isset( $options['local_gtag_file_modified_at'] ) ) { |
||
173 | $options['local_gtag_file_modified_at'] = ''; |
||
174 | } |
||
175 | } |
||
176 | |||
177 | wp_send_json( $options ); |
||
178 | |||
179 | } |
||
180 | |||
181 | /** |
||
182 | * Ajax handler for updating the settings. |
||
183 | */ |
||
184 | public function update_settings() { |
||
185 | |||
186 | check_ajax_referer( 'mi-admin-nonce', 'nonce' ); |
||
187 | |||
188 | if ( ! current_user_can( 'monsterinsights_save_settings' ) ) { |
||
189 | return; |
||
190 | } |
||
191 | |||
192 | if ( isset( $_POST['setting'] ) ) { |
||
193 | $setting = sanitize_text_field( wp_unslash( $_POST['setting'] ) ); |
||
194 | if ( isset( $_POST['value'] ) ) { |
||
195 | $value = $this->handle_sanitization( $setting, $_POST['value'] ); // phpcs:ignore |
||
196 | monsterinsights_update_option( $setting, $value ); |
||
197 | do_action( 'monsterinsights_after_update_settings', $setting, $value ); |
||
198 | } else { |
||
199 | monsterinsights_update_option( $setting, false ); |
||
200 | do_action( 'monsterinsights_after_update_settings', $setting, false ); |
||
201 | } |
||
202 | } |
||
203 | |||
204 | wp_send_json_success(); |
||
205 | |||
206 | } |
||
207 | |||
208 | /** |
||
209 | * Ajax handler for updating the settings. |
||
210 | */ |
||
211 | public function update_settings_bulk() { |
||
229 | |||
230 | } |
||
231 | |||
232 | /** |
||
233 | * Sanitization specific to each field. |
||
234 | * |
||
235 | * @param string $field The key of the field to sanitize. |
||
236 | * @param string $value The value of the field to sanitize. |
||
237 | * |
||
238 | * @return mixed The sanitized input. |
||
239 | */ |
||
240 | private function handle_sanitization( $field, $value ) { |
||
280 | |||
281 | } |
||
282 | |||
283 | /** |
||
284 | * Return the state of the addons ( installed, activated ) |
||
285 | */ |
||
286 | public function get_addons() { |
||
796 | } |
||
797 | |||
798 | /** |
||
799 | * Wrapper around the monsterinsights_get_addon function. |
||
800 | * Kept for backwards compatibility. |
||
801 | * |
||
802 | * @param $installed_plugins |
||
803 | * @param $addons_type |
||
804 | * @param $addon |
||
805 | * @param $slug |
||
806 | * @deprecated Use monsterinsights_get_addon instead. |
||
807 | * @return mixed |
||
808 | */ |
||
809 | public function get_addon( $installed_plugins, $addons_type, $addon, $slug ) { |
||
810 | return monsterinsights_get_addon($installed_plugins, $addons_type, $addon, $slug); |
||
811 | } |
||
812 | |||
813 | /** |
||
814 | * Use custom notices in the Vue app on the Settings screen. |
||
815 | */ |
||
816 | public function hide_old_notices() { |
||
817 | |||
818 | global $wp_version; |
||
819 | if ( version_compare( $wp_version, '4.6', '<' ) ) { |
||
820 | // remove_all_actions triggers an infinite loop on older versions. |
||
821 | return; |
||
822 | } |
||
823 | |||
824 | $screen = get_current_screen(); |
||
825 | // Bail if we're not on a MonsterInsights screen. |
||
826 | if ( empty( $screen->id ) || strpos( $screen->id, 'monsterinsights' ) === false ) { |
||
827 | return; |
||
828 | } |
||
829 | |||
830 | // Hide admin notices on the settings screen. |
||
831 | if ( monsterinsights_is_settings_page() ) { |
||
832 | remove_all_actions( 'admin_notices' ); |
||
833 | } |
||
834 | |||
835 | } |
||
836 | |||
837 | /** |
||
838 | * Update manual v4. |
||
839 | */ |
||
840 | public function update_manual_v4() { |
||
841 | |||
842 | check_ajax_referer( 'mi-admin-nonce', 'nonce' ); |
||
843 | |||
844 | if ( ! current_user_can( 'monsterinsights_save_settings' ) ) { |
||
845 | return; |
||
846 | } |
||
847 | |||
848 | $manual_v4_code = isset( $_POST['manual_v4_code'] ) ? sanitize_text_field( wp_unslash( $_POST['manual_v4_code'] ) ) : ''; |
||
849 | $manual_v4_code = monsterinsights_is_valid_v4_id( $manual_v4_code ); // Also sanitizes the string. |
||
850 | |||
851 | if ( ! empty( $_REQUEST['isnetwork'] ) && sanitize_text_field( wp_unslash( $_REQUEST['isnetwork'] ) ) ) { |
||
852 | define( 'WP_NETWORK_ADMIN', true ); |
||
853 | } |
||
854 | $manual_v4_code_old = is_network_admin() ? MonsterInsights()->auth->get_network_manual_v4_id() : MonsterInsights()->auth->get_manual_v4_id(); |
||
855 | |||
856 | if ( $manual_v4_code && $manual_v4_code_old && $manual_v4_code_old === $manual_v4_code ) { |
||
857 | // Same code we had before |
||
858 | // Do nothing. |
||
859 | wp_send_json_success(); |
||
860 | } else if ( $manual_v4_code && $manual_v4_code_old && $manual_v4_code_old !== $manual_v4_code ) { |
||
861 | // Different UA code. |
||
862 | if ( is_network_admin() ) { |
||
863 | MonsterInsights()->auth->set_network_manual_v4_id( $manual_v4_code ); |
||
864 | } else { |
||
865 | MonsterInsights()->auth->set_manual_v4_id( $manual_v4_code ); |
||
866 | } |
||
867 | } else if ( $manual_v4_code && empty( $manual_v4_code_old ) ) { |
||
868 | // Move to manual. |
||
869 | if ( is_network_admin() ) { |
||
870 | MonsterInsights()->auth->set_network_manual_v4_id( $manual_v4_code ); |
||
871 | } else { |
||
872 | MonsterInsights()->auth->set_manual_v4_id( $manual_v4_code ); |
||
873 | } |
||
874 | } else if ( empty( $manual_v4_code ) && $manual_v4_code_old ) { |
||
875 | // Deleted manual. |
||
876 | if ( is_network_admin() ) { |
||
877 | MonsterInsights()->auth->delete_network_manual_v4_id(); |
||
878 | } else { |
||
879 | MonsterInsights()->auth->delete_manual_v4_id(); |
||
880 | } |
||
881 | } else if ( isset( $_POST['manual_v4_code'] ) && empty( $manual_v4_code ) ) { |
||
882 | wp_send_json_error( array( |
||
883 | 'v4_error' => 1, |
||
884 | // Translators: link tag starts with url, link tag ends. |
||
885 | 'error' => sprintf( |
||
886 | __( 'Oops! Please enter a valid Google Analytics 4 Measurement ID. %1$sLearn how to find your Measurement ID%2$s.', 'google-analytics-for-wordpress' ), |
||
887 | '<a target="_blank" href="' . monsterinsights_get_url( 'notice', 'invalid-manual-gav4-code', 'https://www.monsterinsights.com/docs/how-to-set-up-dual-tracking/' ) . '">', |
||
888 | '</a>' |
||
889 | ), |
||
890 | ) ); |
||
891 | } |
||
892 | |||
893 | wp_send_json_success(); |
||
894 | } |
||
895 | |||
896 | public function update_measurement_protocol_secret() { |
||
897 | check_ajax_referer( 'mi-admin-nonce', 'nonce' ); |
||
898 | |||
899 | if ( ! current_user_can( 'monsterinsights_save_settings' ) ) { |
||
900 | return; |
||
901 | } |
||
902 | |||
903 | if ( ! empty( $_REQUEST['isnetwork'] ) && sanitize_text_field( wp_unslash( $_REQUEST['isnetwork'] ) ) ) { |
||
904 | define( 'WP_NETWORK_ADMIN', true ); |
||
905 | } |
||
906 | |||
907 | $value = empty( $_REQUEST['value'] ) ? '' : sanitize_text_field( wp_unslash( $_REQUEST['value'] ) ); |
||
908 | |||
909 | $auth = MonsterInsights()->auth; |
||
910 | |||
911 | if ( is_network_admin() ) { |
||
912 | $auth->set_network_measurement_protocol_secret( $value ); |
||
913 | } else { |
||
914 | $auth->set_measurement_protocol_secret( $value ); |
||
915 | } |
||
916 | |||
917 | // Send API request to Relay |
||
918 | // TODO: Remove when token automation API is ready |
||
919 | $api = new MonsterInsights_API_Request( 'auth/mp-token/', 'POST' ); |
||
920 | $api->set_additional_data( array( |
||
921 | 'mp_token' => $value, |
||
922 | ) ); |
||
923 | |||
924 | // Even if there's an error from Relay, we can still return a successful json |
||
925 | // payload because we can try again with Relay token push in the future |
||
926 | $data = array(); |
||
927 | $result = $api->request(); |
||
928 | if ( is_wp_error( $result ) ) { |
||
929 | // Just need to output the error in the response for debugging purpose |
||
930 | $data['error'] = array( |
||
931 | 'message' => $result->get_error_message(), |
||
932 | 'code' => $result->get_error_code(), |
||
933 | ); |
||
934 | } |
||
935 | |||
936 | wp_send_json_success( $data ); |
||
937 | } |
||
938 | |||
939 | |||
940 | /** |
||
941 | * Import exported JSON file. |
||
942 | */ |
||
943 | public function handle_settings_import() { |
||
1007 | |||
1008 | } |
||
1009 | |||
1010 | /** |
||
1011 | * Generic Ajax handler for grabbing report data in JSON. |
||
1012 | */ |
||
1013 | public function get_report_data() { |
||
1014 | |||
1015 | check_ajax_referer( 'mi-admin-nonce', 'nonce' ); |
||
1016 | |||
1017 | if ( ! current_user_can( 'monsterinsights_view_dashboard' ) ) { |
||
1018 | // Translators: link tag starts with url, link tag ends. |
||
1019 | $message = sprintf( |
||
1020 | esc_html__( 'Oops! You don not have permissions to view MonsterInsights reporting. Please check with your site administrator that your role is included in the MonsterInsights permissions settings. %1$sClick here for more information%2$s.', 'google-analytics-for-wordpress' ), |
||
1021 | '<a target="_blank" href="' . monsterinsights_get_url( 'notice', 'cannot-view-reports', 'https://www.monsterinsights.com/docs/how-to-allow-user-roles-to-access-the-monsterinsights-reports-and-settings/' ) . '">', |
||
1022 | '</a>' |
||
1023 | ); |
||
1024 | wp_send_json_error( array( 'message' => $message ) ); |
||
1025 | } |
||
1026 | |||
1027 | if ( ! empty( $_REQUEST['isnetwork'] ) && $_REQUEST['isnetwork'] ) { |
||
1028 | define( 'WP_NETWORK_ADMIN', true ); |
||
1029 | } |
||
1030 | $settings_page = admin_url( 'admin.php?page=monsterinsights_settings' ); |
||
1031 | $reactivation_url = monsterinsights_get_url( 'admin-notices', 'expired-license', "https://www.monsterinsights.com/my-account/" ); |
||
1032 | $learn_more_link = esc_url( 'https://www.monsterinsights.com/docs/faq/#licensedplugin' ); |
||
1033 | |||
1034 | // Only for Pro users, require a license key to be entered first so we can link to things. |
||
1035 | if ( monsterinsights_is_pro_version() ) { |
||
1036 | if ( ! MonsterInsights()->license->is_site_licensed() && ! MonsterInsights()->license->is_network_licensed() ) { |
||
1037 | // Translators: Support link tag starts with url and Support link tag ends. |
||
1038 | $message = sprintf( |
||
1039 | esc_html__( 'Oops! You cannot view MonsterInsights reports because you are not licensed. Please try again in a few minutes. If the issue continues, please %1$scontact our support%2$s team.', 'google-analytics-for-wordpress' ), |
||
1040 | '<a target="_blank" href="' . monsterinsights_get_url( 'notice', 'cannot-view-reports', 'https://www.monsterinsights.com/my-account/support/' ) . '">', |
||
1041 | '</a>' |
||
1042 | ); |
||
1043 | wp_send_json_error( array( |
||
1044 | 'message' => $message, |
||
1045 | 'footer' => '<a href="' . $settings_page . '">' . __( 'Add your license', 'google-analytics-for-wordpress' ) . '</a>', |
||
1046 | ) ); |
||
1047 | } else if ( MonsterInsights()->license->is_site_licensed() && ! MonsterInsights()->license->site_license_has_error() ) { |
||
1048 | // Good to go: site licensed. |
||
1049 | } else if ( MonsterInsights()->license->is_network_licensed() && ! MonsterInsights()->license->network_license_has_error() ) { |
||
1050 | // Good to go: network licensed. |
||
1051 | } else { |
||
1052 | // Translators: Support link tag starts with url and Support link tag ends. |
||
1053 | $message = sprintf( |
||
1054 | esc_html__( 'Oops! We had a problem due to a license key error. Please try again in a few minutes. If the problem persists, please %1$scontact our support%2$s team.', 'google-analytics-for-wordpress' ), |
||
1055 | '<a target="_blank" href="' . monsterinsights_get_url( 'notice', 'cannot-view-reports', 'https://www.monsterinsights.com/my-account/support/' ) . '">', |
||
1056 | '</a>' |
||
1057 | ); |
||
1058 | wp_send_json_error( array( 'message' => $message ) ); |
||
1059 | } |
||
1060 | } |
||
1061 | |||
1062 | // We do not have a current auth. |
||
1063 | $site_auth = MonsterInsights()->auth->get_viewname(); |
||
1064 | $ms_auth = is_multisite() && MonsterInsights()->auth->get_network_viewname(); |
||
1065 | if ( ! $site_auth && ! $ms_auth ) { |
||
1066 | $url = admin_url( 'admin.php?page=monsterinsights-onboarding' ); |
||
1067 | |||
1068 | // Check for MS dashboard |
||
1069 | if ( is_network_admin() ) { |
||
1070 | $url = network_admin_url( 'admin.php?page=monsterinsights-onboarding' ); |
||
1071 | } |
||
1072 | // Translators: Wizard link tag starts with url and Wizard link tag ends. |
||
1073 | $message = sprintf( |
||
1074 | esc_html__( 'You need to authenticate into MonsterInsights before viewing reports. Please run our %1$ssetup wizard%2$s.', 'google-analytics-for-wordpress' ), |
||
1075 | '<a href="' . esc_url( $url ) . '">', |
||
1076 | '</a>' |
||
1077 | ); |
||
1078 | wp_send_json_error( array( 'message' => $message ) ); |
||
1079 | } |
||
1080 | |||
1081 | $report_name = isset( $_POST['report'] ) ? sanitize_text_field( wp_unslash( $_POST['report'] ) ) : ''; |
||
1082 | |||
1083 | if ( empty( $report_name ) ) { |
||
1084 | // Translators: Support link tag starts with url and Support link tag ends. |
||
1085 | $message = sprintf( |
||
1086 | esc_html__( 'Oops! We ran into a problem displaying this report. Please %1$scontact our support%2$s team if this issue persists.', 'google-analytics-for-wordpress' ), |
||
1087 | '<a target="_blank" href="' . monsterinsights_get_url( 'notice', 'cannot-display-reports', 'https://www.monsterinsights.com/my-account/support/' ) . '">', |
||
1088 | '</a>' |
||
1089 | ); |
||
1090 | wp_send_json_error( array( 'message' => $message ) ); |
||
1091 | } |
||
1092 | |||
1093 | $report = MonsterInsights()->reporting->get_report( $report_name ); |
||
1094 | |||
1095 | $isnetwork = ! empty( $_REQUEST['isnetwork'] ) ? sanitize_text_field( wp_unslash( $_REQUEST['isnetwork'] ) ) : ''; |
||
1096 | $start = ! empty( $_POST['start'] ) ? sanitize_text_field( wp_unslash( $_POST['start'] ) ) : $report->default_start_date(); |
||
1097 | $end = ! empty( $_POST['end'] ) ? sanitize_text_field( wp_unslash( $_POST['end'] ) ) : $report->default_end_date(); |
||
1098 | |||
1099 | $args = array( |
||
1100 | 'start' => $start, |
||
1101 | 'end' => $end, |
||
1102 | ); |
||
1103 | |||
1104 | // User want to show compare report. |
||
1105 | if ( isset( $_POST['compare_report'] ) ) { |
||
1106 | $args['compare_start'] = ! empty( $_POST['compare_start'] ) ? sanitize_text_field( wp_unslash( $_POST['compare_start'] ) ) : $report->default_compare_start_date(); |
||
1107 | $args['compare_end'] = ! empty( $_POST['compare_end'] ) ? sanitize_text_field( wp_unslash( $_POST['compare_end'] ) ) : $report->default_compare_end_date(); |
||
1108 | } |
||
1109 | |||
1110 | if ( $isnetwork ) { |
||
1111 | $args['network'] = true; |
||
1112 | } |
||
1113 | |||
1114 | if ( monsterinsights_is_pro_version() && ! MonsterInsights()->license->license_can( $report->level ) ) { |
||
1115 | $data = array( |
||
1116 | 'success' => false, |
||
1117 | 'error' => 'license_level', |
||
1118 | ); |
||
1119 | } else { |
||
1120 | $data = apply_filters( 'monsterinsights_vue_reports_data', $report->get_data( $args ), $report_name, $report ); |
||
1121 | } |
||
1122 | |||
1123 | if ( ! empty( $data['success'] ) ) { |
||
1124 | if ( empty( $data['data'] ) ) { |
||
1125 | wp_send_json_success( new stdclass() ); |
||
1126 | } else { |
||
1127 | wp_send_json_success( $data['data'] ); |
||
1128 | } |
||
1129 | } else if ( isset( $data['success'] ) && false === $data['success'] && ! empty( $data['error'] ) ) { |
||
1130 | // Use a custom handler for invalid_grant errors. |
||
1131 | if ( strpos( $data['error'], 'invalid_grant' ) > 0 ) { |
||
1132 | wp_send_json_error( |
||
1133 | array( |
||
1134 | 'message' => 'invalid_grant', |
||
1135 | 'footer' => '', |
||
1136 | ) |
||
1137 | ); |
||
1138 | } |
||
1139 | |||
1140 | wp_send_json_error( |
||
1141 | array( |
||
1142 | 'message' => $data['error'], |
||
1143 | 'footer' => isset( $data['data']['footer'] ) ? $data['data']['footer'] : '', |
||
1144 | 'type' => isset( $data['data']['type'] ) ? $data['data']['type'] : '', |
||
1145 | ) |
||
1146 | ); |
||
1147 | } |
||
1148 | |||
1149 | // Translators: Support link tag starts with url and Support link tag ends. |
||
1150 | $message = sprintf( |
||
1151 | esc_html__( 'Oops! We encountered an error while generating your reports. Please wait a few minutes and try again. If the issue persists, please %1$scontact our support%2$s team.', 'google-analytics-for-wordpress' ), |
||
1152 | '<a href="' . monsterinsights_get_url( 'notice', 'error-generating-reports', 'https://www.monsterinsights.com/my-account/support/' ) . '">', |
||
1153 | '</a>' |
||
1154 | ); |
||
1155 | wp_send_json_error( array( 'message' => $message ) ); |
||
1156 | } |
||
1157 | |||
1158 | /** |
||
1159 | * Install plugins which are not addons. |
||
1160 | */ |
||
1161 | public function install_plugin() { |
||
1162 | check_ajax_referer( 'mi-admin-nonce', 'nonce' ); |
||
1163 | |||
1164 | if ( ! monsterinsights_can_install_plugins() ) { |
||
1165 | wp_send_json( array( |
||
1166 | 'error' => esc_html__( 'Oops! You are not allowed to install plugins. Please contact your website administrator for further assistance.', 'google-analytics-for-wordpress' ), |
||
1167 | ) ); |
||
1168 | } |
||
1169 | |||
1170 | $slug = isset( $_POST['slug'] ) ? sanitize_text_field( wp_unslash( $_POST['slug'] ) ) : false; |
||
1171 | |||
1172 | if ( ! $slug ) { |
||
1173 | wp_send_json( array( |
||
1174 | 'message' => esc_html__( 'Missing plugin name.', 'google-analytics-for-wordpress' ), |
||
1175 | ) ); |
||
1176 | } |
||
1177 | |||
1178 | include_once ABSPATH . 'wp-admin/includes/plugin-install.php'; |
||
1179 | |||
1180 | $api = plugins_api( 'plugin_information', array( |
||
1181 | 'slug' => $slug, |
||
1182 | 'fields' => array( |
||
1183 | 'short_description' => false, |
||
1184 | 'sections' => false, |
||
1185 | 'requires' => false, |
||
1186 | 'rating' => false, |
||
1187 | 'ratings' => false, |
||
1188 | 'downloaded' => false, |
||
1189 | 'last_updated' => false, |
||
1190 | 'added' => false, |
||
1191 | 'tags' => false, |
||
1192 | 'compatibility' => false, |
||
1193 | 'homepage' => false, |
||
1194 | 'donate_link' => false, |
||
1195 | ), |
||
1196 | ) ); |
||
1197 | |||
1198 | if ( is_wp_error( $api ) ) { |
||
1199 | return $api->get_error_message(); |
||
1200 | } |
||
1201 | |||
1202 | $download_url = $api->download_link; |
||
1203 | |||
1204 | $method = ''; |
||
1205 | $url = add_query_arg( |
||
1206 | array( |
||
1207 | 'page' => 'monsterinsights-settings', |
||
1208 | ), |
||
1209 | admin_url( 'admin.php' ) |
||
1210 | ); |
||
1211 | $url = esc_url( $url ); |
||
1212 | |||
1213 | ob_start(); |
||
1214 | if ( false === ( $creds = request_filesystem_credentials( $url, $method, false, false, null ) ) ) { |
||
1215 | $form = ob_get_clean(); |
||
1216 | |||
1217 | wp_send_json( array( 'form' => $form ) ); |
||
1218 | } |
||
1219 | |||
1220 | // If we are not authenticated, make it happen now. |
||
1221 | if ( ! WP_Filesystem( $creds ) ) { |
||
1222 | ob_start(); |
||
1223 | request_filesystem_credentials( $url, $method, true, false, null ); |
||
1224 | $form = ob_get_clean(); |
||
1225 | |||
1226 | wp_send_json( array( 'form' => $form ) ); |
||
1227 | |||
1228 | } |
||
1229 | |||
1230 | // We do not need any extra credentials if we have gotten this far, so let's install the plugin. |
||
1231 | monsterinsights_require_upgrader(); |
||
1232 | |||
1233 | // Prevent language upgrade in ajax calls. |
||
1234 | remove_action( 'upgrader_process_complete', array( 'Language_Pack_Upgrader', 'async_upgrade' ), 20 ); |
||
1235 | // Create the plugin upgrader with our custom skin. |
||
1236 | $installer = new MonsterInsights_Plugin_Upgrader( new MonsterInsights_Skin() ); |
||
1237 | $installer->install( $download_url ); |
||
1238 | |||
1239 | // Flush the cache and return the newly installed plugin basename. |
||
1240 | wp_cache_flush(); |
||
1241 | wp_send_json_success(); |
||
1242 | |||
1243 | wp_die(); |
||
1244 | } |
||
1245 | |||
1246 | /** |
||
1247 | * Store that the first run notice has been dismissed so it doesn't show up again. |
||
1248 | */ |
||
1249 | public function dismiss_first_time_notice() { |
||
1250 | check_ajax_referer( 'mi-admin-nonce', 'nonce' ); |
||
1251 | |||
1252 | if ( ! current_user_can( 'monsterinsights_save_settings' ) ) { |
||
1253 | return; |
||
1254 | } |
||
1255 | |||
1256 | monsterinsights_update_option( 'monsterinsights_first_run_notice', true ); |
||
1257 | |||
1258 | wp_send_json_success(); |
||
1259 | } |
||
1260 | |||
1261 | /** |
||
1262 | * Get the notice status by id. |
||
1263 | */ |
||
1264 | public function get_notice_status() { |
||
1265 | |||
1266 | check_ajax_referer( 'mi-admin-nonce', 'nonce' ); |
||
1267 | |||
1268 | $notice_id = empty( $_POST['notice'] ) ? false : sanitize_text_field( wp_unslash( $_POST['notice'] ) ); |
||
1269 | if ( ! $notice_id ) { |
||
1270 | wp_send_json_error(); |
||
1271 | } |
||
1272 | $is_dismissed = MonsterInsights()->notices->is_dismissed( $notice_id ); |
||
1273 | |||
1274 | wp_send_json_success( array( |
||
1275 | 'dismissed' => $is_dismissed, |
||
1276 | ) ); |
||
1277 | } |
||
1278 | |||
1279 | /** |
||
1280 | * Dismiss notices by id. |
||
1281 | */ |
||
1282 | public function dismiss_notice() { |
||
1283 | |||
1284 | check_ajax_referer( 'mi-admin-nonce', 'nonce' ); |
||
1285 | |||
1286 | $notice_id = empty( $_POST['notice'] ) ? false : sanitize_text_field( wp_unslash( $_POST['notice'] ) ); |
||
1287 | if ( ! $notice_id ) { |
||
1288 | wp_send_json_error(); |
||
1289 | } |
||
1290 | MonsterInsights()->notices->dismiss( $notice_id ); |
||
1291 | |||
1292 | wp_send_json_success(); |
||
1293 | } |
||
1294 | |||
1295 | /** |
||
1296 | * Retrieve posts/pages |
||
1297 | * |
||
1298 | * @access admin |
||
1299 | * @since 3.0.0 |
||
1300 | */ |
||
1301 | public function get_posts() { |
||
1302 | |||
1303 | // Run a security check first. |
||
1304 | check_ajax_referer( 'mi-admin-nonce', 'nonce' ); |
||
1305 | |||
1306 | $post_type = isset( $_POST['post_type'] ) ? sanitize_text_field( wp_unslash( $_POST['post_type'] ) ) : 'any'; |
||
1307 | |||
1308 | $already_added = monsterinsights_get_option('popular_posts_inline_curated', []); |
||
1309 | $exclude = array(); |
||
1310 | if( is_array( $already_added ) && !empty( $already_added ) ){ |
||
1311 | foreach ( $already_added as $key => $value ) { |
||
1312 | $exclude[$value['id']] = $value['id']; |
||
1313 | } |
||
1314 | } |
||
1315 | |||
1316 | $exclude = array_unique(array_values($exclude)); |
||
1317 | |||
1318 | $args = array( |
||
1319 | 's' => isset( $_POST['keyword'] ) ? sanitize_text_field( wp_unslash( $_POST['keyword'] ) ) : '', |
||
1320 | 'post_type' => $post_type, |
||
1321 | 'posts_per_page' => isset( $_POST['numberposts'] ) ? sanitize_text_field( wp_unslash( $_POST['numberposts'] ) ) : 25, |
||
1322 | 'orderby' => 'post_title', |
||
1323 | 'order' => 'ASC', |
||
1324 | 'post__not_in' => $exclude, |
||
1325 | ); |
||
1326 | |||
1327 | $array = array(); |
||
1328 | $posts = get_posts( $args ); |
||
1329 | |||
1330 | if ( in_array( $post_type, array( 'page', 'any' ), true ) ) { |
||
1331 | $homepage = get_option( 'page_on_front' ); |
||
1332 | if ( ! $homepage ) { |
||
1333 | $array[] = array( |
||
1334 | 'id' => - 1, |
||
1335 | 'title' => __( 'Homepage', 'google-analytics-for-wordpress' ), |
||
1336 | ); |
||
1337 | } |
||
1338 | } |
||
1339 | |||
1340 | if ( $posts ) { |
||
1341 | foreach ( $posts as $post ) { |
||
1342 | $array[] = array( |
||
1343 | 'id' => $post->ID, |
||
1344 | 'title' => $post->post_title, |
||
1345 | ); |
||
1346 | } |
||
1347 | } |
||
1348 | |||
1349 | wp_send_json_success( $array ); |
||
1350 | } |
||
1351 | |||
1352 | /** |
||
1353 | * Search for taxonomy terms. |
||
1354 | * |
||
1355 | * @access admin |
||
1356 | * @since 3.0.0 |
||
1357 | */ |
||
1358 | public function get_taxonomy_terms() { |
||
1359 | |||
1360 | // Run a security check first. |
||
1361 | check_ajax_referer( 'mi-admin-nonce', 'nonce' ); |
||
1362 | |||
1363 | $keyword = isset( $_POST['keyword'] ) ? sanitize_text_field( wp_unslash( $_POST['keyword'] ) ) : ''; |
||
1364 | $taxonomy = isset( $_POST['taxonomy'] ) ? sanitize_text_field( wp_unslash( $_POST['taxonomy'] ) ) : 'category'; |
||
1365 | |||
1366 | $args = array( |
||
1367 | 'taxonomy' => array( $taxonomy ), |
||
1368 | 'hide_empty' => false, |
||
1369 | 'name__like' => $keyword, |
||
1370 | ); |
||
1371 | |||
1372 | $terms = get_terms( $args ); |
||
1373 | $array = array(); |
||
1374 | |||
1375 | if ( ! empty( $terms ) ) { |
||
1376 | foreach ( $terms as $term ) { |
||
1377 | $array[] = array( |
||
1378 | 'id' => esc_attr( $term->term_id ), |
||
1379 | 'text' => esc_attr( $term->name ), |
||
1380 | ); |
||
1381 | } |
||
1382 | } |
||
1383 | |||
1384 | wp_send_json_success( $array ); |
||
1385 | } |
||
1386 | |||
1387 | /** |
||
1388 | * Get the post types in a name => Label array. |
||
1389 | */ |
||
1390 | public function get_post_types() { |
||
1391 | |||
1392 | // Run a security check first. |
||
1393 | check_ajax_referer( 'mi-admin-nonce', 'nonce' ); |
||
1394 | |||
1395 | $post_types_args = array( |
||
1396 | 'public' => true, |
||
1397 | ); |
||
1398 | $post_types = get_post_types( $post_types_args, 'objects' ); |
||
1399 | |||
1400 | $post_types_parsed = array(); |
||
1401 | |||
1402 | foreach ( $post_types as $post_type ) { |
||
1403 | // Exclude post types that don't support the content editor. |
||
1404 | // Exclude the WooCommerce product post type as that doesn't use the "the_content" filter and we can't auto-add popular posts to it. |
||
1405 | if ( ! post_type_supports( $post_type->name, 'editor' ) || 'product' === $post_type->name ) { |
||
1406 | continue; |
||
1407 | } |
||
1408 | $post_types_parsed[ $post_type->name ] = $post_type->labels->singular_name; |
||
1409 | } |
||
1410 | |||
1411 | $post_types_parsed = apply_filters( 'monsterinsights_vue_post_types_editor', $post_types_parsed ); |
||
1412 | |||
1413 | wp_send_json( $post_types_parsed ); |
||
1414 | |||
1415 | } |
||
1416 | |||
1417 | |||
1418 | public function check_popular_posts_report() { |
||
1419 | |||
1420 | check_ajax_referer( 'mi-admin-nonce', 'nonce' ); |
||
1421 | |||
1422 | if ( ! current_user_can( 'monsterinsights_view_dashboard' ) ) { |
||
1423 | // Translators: Link tag starts with url and link tag ends. |
||
1424 | $message = sprintf( |
||
1425 | esc_html__( 'Oops! You don not have permissions to view or access Popular Posts. Please check with your site administrator that your role is included in the MonsterInsights permissions settings. %1$sClick here for more information%2$s.', 'google-analytics-for-wordpress' ), |
||
1426 | '<a target="_blank" href="' . monsterinsights_get_url( 'notice', 'cannot-view-dashboard', 'https://www.monsterinsights.com/docs/how-to-allow-user-roles-to-access-the-monsterinsights-reports-and-settings/' ) . '">', |
||
1427 | '</a>' |
||
1428 | ); |
||
1429 | wp_send_json_error( array( 'message' => $message ) ); |
||
1430 | } |
||
1431 | |||
1432 | if ( ! empty( $_REQUEST['isnetwork'] ) && $_REQUEST['isnetwork'] ) { |
||
1433 | define( 'WP_NETWORK_ADMIN', true ); |
||
1434 | } |
||
1435 | $settings_page = admin_url( 'admin.php?page=monsterinsights_settings' ); |
||
1436 | |||
1437 | // Only for Pro users, require a license key to be entered first so we can link to things. |
||
1438 | if ( monsterinsights_is_pro_version() ) { |
||
1439 | if ( ! MonsterInsights()->license->is_site_licensed() && ! MonsterInsights()->license->is_network_licensed() ) { |
||
1440 | $url = admin_url( 'admin.php?page=monsterinsights_settings#/' ); |
||
1441 | |||
1442 | // Check for MS dashboard |
||
1443 | if ( is_network_admin() ) { |
||
1444 | $url = network_admin_url( 'admin.php?page=monsterinsights_settings#/' ); |
||
1445 | } |
||
1446 | // Translators: Setting page link tag starts with url and Setting page link tag ends. |
||
1447 | $message = sprintf( |
||
1448 | esc_html__( 'Oops! We could not find a valid license key for MonsterInsights. Please %1$senter a valid license key%2$s to view this report.', 'google-analytics-for-wordpress' ), |
||
1449 | '<a href="' . esc_url( $url ) . '">', |
||
1450 | '</a>' |
||
1451 | ); |
||
1452 | wp_send_json_error( array( |
||
1453 | 'message' => $message, |
||
1454 | 'footer' => '<a href="' . $settings_page . '">' . __( 'Add your license', 'google-analytics-for-wordpress' ) . '</a>', |
||
1455 | ) ); |
||
1456 | } else if ( MonsterInsights()->license->is_site_licensed() && ! MonsterInsights()->license->site_license_has_error() ) { |
||
1457 | // Good to go: site licensed. |
||
1458 | } else if ( MonsterInsights()->license->is_network_licensed() && ! MonsterInsights()->license->network_license_has_error() ) { |
||
1459 | // Good to go: network licensed. |
||
1460 | } else { |
||
1461 | // Translators: Account page link tag starts with url and Account page link tag ends. |
||
1462 | $message = sprintf( |
||
1463 | esc_html__( 'Oops! We could not find a valid license key. Please enter a valid license key to view this report. You can find your license by logging into your %1$sMonsterInsights account%2$s.', 'google-analytics-for-wordpress' ), |
||
1464 | '<a target="_blank" href="' . monsterinsights_get_url( 'notice', 'license-errors', 'https://www.monsterinsights.com/my-account/licenses/' ) . '">', |
||
1465 | '</a>' |
||
1466 | ); |
||
1467 | wp_send_json_error( array( 'message' => $message ) ); |
||
1468 | } |
||
1469 | } |
||
1470 | |||
1471 | // We do not have a current auth. |
||
1472 | $site_auth = MonsterInsights()->auth->get_viewname(); |
||
1473 | $ms_auth = is_multisite() && MonsterInsights()->auth->get_network_viewname(); |
||
1474 | if ( ! $site_auth && ! $ms_auth ) { |
||
1475 | $url = admin_url( 'admin.php?page=monsterinsights_settings#/' ); |
||
1476 | |||
1477 | // Check for MS dashboard |
||
1478 | if ( is_network_admin() ) { |
||
1479 | $url = network_admin_url( 'admin.php?page=monsterinsights_settings#/' ); |
||
1480 | } |
||
1481 | // Translators: Wizard page link tag starts with url and Wizard page link tag ends. |
||
1482 | $message = sprintf( |
||
1483 | esc_html__( 'You need to authenticate into MonsterInsights before viewing reports. Please complete the setup by going through our %1$ssetup wizard%2$s.', 'google-analytics-for-wordpress' ), |
||
1484 | '<a href="' . esc_url( $url ) . '">', |
||
1485 | '</a>' |
||
1486 | ); |
||
1487 | wp_send_json_error( array( 'message' => $message ) ); |
||
1488 | } |
||
1489 | |||
1490 | $report_name = 'popularposts'; |
||
1491 | |||
1492 | if ( empty( $report_name ) ) { |
||
1493 | // Translators: Support link tag starts with url and Support link tag ends. |
||
1494 | $message = sprintf( |
||
1495 | esc_html__( 'Oops! We encountered an error while generating your reports. Please wait a few minutes and try again. If the issue persists, please %1$scontact our support%2$s team.', 'google-analytics-for-wordpress' ), |
||
1496 | '<a target="_blank" href="' . monsterinsights_get_url( 'notice', 'cannot-generate-reports', 'https://www.monsterinsights.com/my-account/support/' ) . '">', |
||
1497 | '</a>' |
||
1498 | ); |
||
1499 | wp_send_json_error( array( 'message' => $message ) ); |
||
1500 | } |
||
1501 | |||
1502 | $report = MonsterInsights()->reporting->get_report( $report_name ); |
||
1503 | |||
1504 | $isnetwork = ! empty( $_REQUEST['isnetwork'] ) ? sanitize_text_field( wp_unslash( $_REQUEST['isnetwork'] ) ) : ''; |
||
1505 | $start = ! empty( $_POST['start'] ) ? sanitize_text_field( wp_unslash( $_POST['start'] ) ) : $report->default_start_date(); |
||
1506 | $end = ! empty( $_POST['end'] ) ? sanitize_text_field( wp_unslash( $_POST['end'] ) ) : $report->default_end_date(); |
||
1507 | |||
1508 | $args = array( |
||
1509 | 'start' => $start, |
||
1510 | 'end' => $end, |
||
1511 | ); |
||
1512 | |||
1513 | if ( $isnetwork ) { |
||
1514 | $args['network'] = true; |
||
1515 | } |
||
1516 | |||
1517 | if ( monsterinsights_is_pro_version() && ! MonsterInsights()->license->license_can( $report->level ) ) { |
||
1518 | $data = array( |
||
1519 | 'success' => false, |
||
1520 | 'error' => 'license_level', |
||
1521 | ); |
||
1522 | } else { |
||
1523 | $data = apply_filters( 'monsterinsights_vue_reports_data', $report->get_data( $args ), $report_name, $report ); |
||
1524 | } |
||
1525 | |||
1526 | if ( ! empty( $data['success'] ) && ! empty( $data['data'] ) ) { |
||
1527 | wp_send_json_success( $data['data'] ); |
||
1528 | } else if ( isset( $data['success'] ) && false === $data['success'] && ! empty( $data['error'] ) ) { |
||
1529 | // Use a custom handler for invalid_grant errors. |
||
1530 | if ( strpos( $data['error'], 'invalid_grant' ) > 0 ) { |
||
1531 | wp_send_json_error( |
||
1532 | array( |
||
1533 | 'message' => 'invalid_grant', |
||
1534 | 'footer' => '', |
||
1535 | ) |
||
1536 | ); |
||
1537 | } |
||
1538 | |||
1539 | wp_send_json_error( |
||
1540 | array( |
||
1541 | 'message' => $data['error'], |
||
1542 | 'footer' => isset( $data['data']['footer'] ) ? $data['data']['footer'] : '', |
||
1543 | ) |
||
1544 | ); |
||
1545 | } |
||
1546 | |||
1547 | // Translators: Support link tag starts with url and Support link tag ends. |
||
1548 | $message = sprintf( |
||
1549 | __( 'Oops! We encountered an error while generating your reports. Please wait a few minutes and try again. If the issue persists, please %1$scontact our support%2$s team.', 'google-analytics-for-wordpress' ), |
||
1550 | '<a target="_blank" href="' . monsterinsights_get_url( 'notice', 'cannot-generate-reports', 'https://www.monsterinsights.com/my-account/support/' ) . '">', |
||
1551 | '</a>' |
||
1552 | ); |
||
1553 | wp_send_json_error( array( 'message' => $message ) ); |
||
1554 | } |
||
1555 | |||
1556 | /** |
||
1557 | * Ajax handler for popular posts theme customization settings. |
||
1558 | * Specific theme styles are stored separately so we can handle 20+ themes with their specific settings. |
||
1559 | */ |
||
1560 | public function update_popular_posts_theme_setting() { |
||
1561 | |||
1562 | check_ajax_referer( 'mi-admin-nonce', 'nonce' ); |
||
1563 | |||
1564 | if ( ! current_user_can( 'monsterinsights_save_settings' ) ) { |
||
1565 | return; |
||
1566 | } |
||
1567 | |||
1568 | if ( ! empty( $_POST['type'] ) && ! empty( $_POST['theme'] ) && ! empty( $_POST['object'] ) && ! empty( $_POST['key'] ) && ! empty( $_POST['value'] ) ) { |
||
1569 | $settings_key = 'monsterinsights_popular_posts_theme_settings'; |
||
1570 | $type = sanitize_text_field( wp_unslash( $_POST['type'] ) ); // Type of Popular Posts instance: inline/widget/products. |
||
1571 | $theme = sanitize_text_field( wp_unslash( $_POST['theme'] ) ); |
||
1572 | $object = sanitize_text_field( wp_unslash( $_POST['object'] ) ); // Style object like title, label, background, etc. |
||
1573 | $key = sanitize_text_field( wp_unslash( $_POST['key'] ) ); // Style key for the object like color, font size, etc. |
||
1574 | $value = sanitize_text_field( wp_unslash( $_POST['value'] ) ); // Value of custom style like 12px or #fff. |
||
1575 | $settings = get_option( $settings_key, array() ); |
||
1576 | |||
1577 | if ( ! isset( $settings[ $type ] ) ) { |
||
1578 | $settings[ $type ] = array(); |
||
1579 | } |
||
1580 | if ( ! isset( $settings[ $type ][ $theme ] ) ) { |
||
1581 | $settings[ $type ][ $theme ] = array(); |
||
1582 | } |
||
1583 | |||
1584 | if ( ! isset( $settings[ $type ][ $theme ][ $object ] ) ) { |
||
1585 | $settings[ $type ][ $theme ][ $object ] = array(); |
||
1586 | } |
||
1587 | |||
1588 | $settings[ $type ][ $theme ][ $object ][ $key ] = $value; |
||
1589 | |||
1590 | update_option( $settings_key, $settings ); |
||
1591 | |||
1592 | wp_send_json_success(); |
||
1593 | } |
||
1594 | |||
1595 | wp_send_json_error(); |
||
1596 | |||
1597 | } |
||
1598 | |||
1599 | /** |
||
1600 | * Import site notes from exported file. |
||
1601 | */ |
||
1602 | private function import_site_notes( $site_notes ) { |
||
1621 | ) ); |
||
1622 | } |
||
1623 | } |
||
1624 | } |
||
1625 |