upgrade-functions.php ➔ gmb_trigger_upgrades()   B
last analyzed

Complexity

Conditions 6
Paths 16

Size

Total Lines 25

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
nc 16
nop 0
dl 0
loc 25
rs 8.8977
c 0
b 0
f 0
1
<?php
0 ignored issues
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 20 and the first side effect is on line 11.

The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.

The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.

To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.

Loading history...
2
/**
3
 *  Handles Upgrade Functionality
4
 *
5
 * @copyright   Copyright (c) 2015, WordImpress
6
 * @since       : 2.0
7
 */
8
9
// Exit if accessed directly
10
if ( ! defined( 'ABSPATH' ) ) {
11
	exit;
12
}
13
14
/**
15
 * Display Upgrade Notices
16
 *
17
 * @since 2.0
18
 * @return void
19
 */
20
function gmb_show_upgrade_notices() {
21
22
	//Uncomment for testing ONLY - Never leave uncommented unless testing:
23
	//delete_option( 'gmb_refid_upgraded' );
0 ignored issues
show
Unused Code Comprehensibility introduced by
58% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
24
25
	// Don't show notices on the upgrades page
26
	if ( isset( $_GET['page'] ) && $_GET['page'] == 'gmb-upgrades' ) {
27
		return;
28
	}
29
30
	//Check to see if we have any posts
31
	$gmb_posts = get_posts( array( 'post_type' => 'google_maps', 'posts_per_page' => 10 ) );
32
	if(empty($gmb_posts)){
33
		update_option( 'gmb_refid_upgraded', 'upgraded' );//mark as updated
34
		return; //Don't run if there's no posts!
35
	}
36
37
	$gmb_version = get_option( 'gmb_version' );
38
39
	if ( ! $gmb_version ) {
40
		// 2.0 is the first version to use this option so we must add it
41
		$gmb_version = '2.0';
42
	}
43
	update_option( 'gmb_version', GMB_VERSION );
44
45
	$gmb_version = preg_replace( '/[^0-9.].*/', '', $gmb_version );
46
47
	if ( version_compare( $gmb_version, '2.0', '<=' ) && ! get_option( 'gmb_refid_upgraded' ) ) {
48
		printf(
49
			'<div class="updated"><p><strong>Google Maps Builder Notice:</strong> ' . esc_html__( 'Google has updated their Maps API to use the new Google Places ID rather than previous Reference ID. The old method will soon be deprecated and eventually go offline. We are being proactive and would like to update your maps to use the new Places ID. Once you upgrade, your maps should work just fine but remember to make a backup prior to upgrading. If you choose not to upgrade Google will eventually take the old reference ID offline (no date has been given). Please contact WordImpress support via our website if you have any further questions or issues. %sClick here to upgrade your maps to use the new Places ID%s', 'gmb' ) . '</p></div>',
50
			'<br><a href="' . esc_url( admin_url( 'options.php?page=gmb-upgrades' ) ) . '" class="button button-primary" style="margin-top:10px;">',
51
			'</a>'
52
		);
53
	}
54
55
56
}
57
58
add_action( 'admin_notices', 'gmb_show_upgrade_notices' );
59
60
61
/**
62
 * Creates the upgrade page
63
 *
64
 * links to global variables
65
 *
66
 * @since 2.0
67
 */
68
function gmb_add_upgrade_submenu_page() {
69
70
	$gmb_upgrades_screen = add_submenu_page( null, __( 'Maps Builder Upgrades', 'gmb' ), __( 'Maps Builder Upgrades', 'gmb' ), 'activate_plugins', 'gmb-upgrades', 'gmb_upgrades_screen' );
71
72
}
73
74
add_action( 'admin_menu', 'gmb_add_upgrade_submenu_page', 10 );
75
76
/**
77
 * Triggers all upgrade functions
78
 *
79
 * This function is usually triggered via AJAX
80
 *
81
 * @since 2.0
82
 * @return void
83
 */
84
function gmb_trigger_upgrades() {
85
86
	if ( ! current_user_can( 'activate_plugins' ) ) {
87
		wp_die( __( 'You do not have permission to do plugin upgrades', 'gmb' ), __( 'Error', 'gmb' ), array( 'response' => 403 ) );
88
	}
89
90
	$gmb_version = get_option( 'gmb_version' );
91
92
	//Is the option above in the db?
93
	if ( ! $gmb_version ) {
94
		// 2.0 is the first version to use this option so we must add it
95
		$gmb_version = '2.0';
96
		add_option( 'gmb_version', $gmb_version );
97
	}
98
99
	if ( version_compare( GMB_VERSION, $gmb_version, '>=' ) && ! get_option( 'gmb_refid_upgraded' ) ) {
100
		gmb_v2_upgrades();
101
	}
102
103
	update_option( 'gmb_version', $gmb_version );
104
105
	if ( DOING_AJAX ) {
106
		die( 'complete' );
0 ignored issues
show
Coding Style Compatibility introduced by
The function gmb_trigger_upgrades() contains an exit expression.

An exit expression should only be used in rare cases. For example, if you write a short command line script.

In most cases however, using an exit expression makes the code untestable and often causes incompatibilities with other libraries. Thus, unless you are absolutely sure it is required here, we recommend to refactor your code to avoid its usage.

Loading history...
107
	} // Let AJAX know that the upgrade is complete
108
}
109
110
add_action( 'wp_ajax_gmb_trigger_upgrades', 'gmb_trigger_upgrades' );
111
112
113
/**
114
 * Upgrade from Google Reference ID to Places ID
115
 *
116
 * @since 2.0
117
 * @uses  WP_Query
118
 * @return void
119
 */
120
function gmb_v2_upgrades() {
121
122
	//Set key variables
123
	$google_api_key = gmb_get_option( 'gmb_api_key' );
124
125
	//Loop through maps
126
	$args = array(
127
		'post_type'      => 'google_maps',
128
		'posts_per_page' => - 1
129
	);
130
131
	// The Query
132
	$the_query = new WP_Query( $args );
133
134
	// The CPT Loop
135
	if ( $the_query->have_posts() ) : while ( $the_query->have_posts() ) : $the_query->the_post();
136
137
		//Repeater markers data
138
		$markers = get_post_meta( get_the_ID(), 'gmb_markers_group', true );
139
140
		//If no markers skip
141
		if ( ! empty( $markers ) ) {
142
143
			//Markers loop
144
			foreach ( $markers as $key => $marker ) {
145
146
				$ref_id   = isset( $marker['reference'] ) ? $marker['reference'] : '';
147
				$place_id = isset( $marker['place_id'] ) ? $marker['place_id'] : '';
148
149
				//No ref ID -> skip; If place_id already there skip
150
				if ( empty( $ref_id ) ) {
151
					continue;
152
				}
153
				if ( ! empty( $place_id ) ) {
154
					continue;
155
				}
156
				//cURL the Google API for the Google Place ID
157
				$google_places_url = add_query_arg(
158
					array(
159
						'reference' => $ref_id,
160
						'key'       => $google_api_key
161
					),
162
					'https://maps.googleapis.com/maps/api/place/details/json'
163
				);
164
165
				$response = wp_remote_get( $google_places_url,
166
					array(
167
						'timeout'   => 15,
168
						'sslverify' => false
169
					)
170
				);
171
172
				// make sure the response came back okay
173
				if ( is_wp_error( $response ) ) {
174
					return;
175
				}
176
177
				// decode the license data
178
				$response = json_decode( $response['body'], true );
179
180
				//Place ID is there, now let's update the widget data
181
				if ( isset( $response['result']['place_id'] ) ) {
182
183
					//Add Place ID to markers array
184
					$markers[ $key ]['place_id'] = $response['result']['place_id'];
185
186
				}
187
188
				//Pause for 2 seconds so we don't overwhelm the Google API with requests
189
				sleep( 2 );
190
191
192
			} //end foreach
193
194
			//Update repeater data with new data
195
			update_post_meta( get_the_ID(), 'gmb_markers_group', $markers );
196
197
		} //endif
198
199
	endwhile; endif;
200
201
	// Reset Post Data
202
	wp_reset_postdata();
203
204
	//Update our options and GTF out
205
	update_option( 'gmb_refid_upgraded', 'upgraded' );
206
207
}
208