1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
// Exit if accessed directly |
4
|
|
|
if ( ! defined( 'ABSPATH' ) ) { |
5
|
|
|
exit; |
6
|
|
|
} |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Checks to see if a session is set and it's appropriate to start one, and starts it if necessary |
10
|
|
|
* @return void |
11
|
|
|
*/ |
12
|
|
|
function ppp_maybe_start_session() { |
13
|
1 |
|
if( !class_exists( 'TwitterOAuth' ) ) { |
14
|
|
|
require_once ( PPP_PATH . '/includes/libs/twitter/twitteroauth.php' ); |
15
|
|
|
} |
16
|
|
|
|
17
|
1 |
|
$ret = false; |
18
|
1 |
|
if ( ( is_admin() || ( defined( 'DOING_CRON' ) && DOING_CRON ) ) && session_status() == PHP_SESSION_NONE && ! headers_sent() ) { |
19
|
|
|
$ret = session_start(); |
20
|
|
|
} |
21
|
|
|
|
22
|
1 |
|
return $ret; |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Returns if a link tracking method is enabled |
27
|
|
|
* @return boolean True if a form of link tracking is enabled, false if not |
28
|
|
|
*/ |
29
|
|
|
function ppp_link_tracking_enabled() { |
30
|
2 |
|
global $ppp_share_settings; |
31
|
2 |
|
$result = false; |
32
|
|
|
|
33
|
2 |
|
if ( isset( $ppp_share_settings['analytics'] ) && $ppp_share_settings['analytics'] !== 'none' ) { |
34
|
2 |
|
$result = true; |
35
|
2 |
|
} |
36
|
|
|
|
37
|
2 |
|
return apply_filters( 'ppp_is_link_tracking_enabled', $result ); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Get a post slug via the ID |
42
|
|
|
* @param int $post_id The post ID |
43
|
|
|
* @return string The slug of the post |
44
|
|
|
*/ |
45
|
|
|
function ppp_get_post_slug_by_id( $post_id ) { |
46
|
5 |
|
$post_data = get_post( $post_id, ARRAY_A ); |
47
|
5 |
|
$slug = $post_data['post_name']; |
48
|
|
|
|
49
|
5 |
|
return $slug; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Get's the array of text replacements |
54
|
|
|
* @return array The array of text replacements, each with a token and description items |
55
|
|
|
*/ |
56
|
|
|
function ppp_get_text_tokens() { |
57
|
3 |
|
return apply_filters( 'ppp_text_tokens', array() ); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Returns the number of says to setup shares for |
62
|
|
|
* @return int The number of days |
63
|
|
|
*/ |
64
|
|
|
function ppp_share_days_count() { |
65
|
|
|
return apply_filters( 'ppp_share_days_count', 6 ); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Returns if the shortener option is chosen |
70
|
|
|
* @return boolean True/False if the shortener has been selected |
71
|
|
|
*/ |
72
|
|
|
function ppp_is_shortener_enabled() { |
73
|
1 |
|
global $ppp_share_settings; |
74
|
|
|
|
75
|
1 |
|
return ( isset( $ppp_share_settings['shortener'] ) && !empty( $ppp_share_settings['shortener'] ) && $ppp_share_settings != '-1' ); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Strips slashes and html_entities_decode for sending to the networks. |
80
|
|
|
*/ |
81
|
|
|
function ppp_entities_and_slashes( $string ) { |
82
|
1 |
|
return stripslashes( html_entity_decode( $string, ENT_COMPAT, 'UTF-8' ) ); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Runs hook for the social networks to add their thumbnail sizes |
87
|
|
|
* @return void |
88
|
|
|
*/ |
89
|
|
|
function ppp_add_image_sizes() { |
90
|
|
|
do_action( 'ppp_add_image_sizes' ); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* Return the array of supported post types |
95
|
|
|
* |
96
|
|
|
* @since 2.3 |
97
|
|
|
* @return array Array of post types in a key/value store |
98
|
|
|
*/ |
99
|
|
|
function ppp_supported_post_types() { |
100
|
1 |
|
$post_type_args = apply_filters( 'ppp_supported_post_type_args', array( |
101
|
1 |
|
'public' => true, |
102
|
1 |
|
'show_ui' => true, |
103
|
1 |
|
) ); |
104
|
1 |
|
$post_types = get_post_types( $post_type_args, NULL, 'and' ); |
105
|
|
|
|
106
|
1 |
|
$unsupported_post_types = array( 'wp_log', 'attachment' ); |
107
|
1 |
|
foreach ( $unsupported_post_types as $unsupported_post_type ) { |
108
|
1 |
|
if ( array_key_exists( $unsupported_post_type, $post_types ) ) { |
109
|
1 |
|
unset( $post_types[ $unsupported_post_type ] ); |
110
|
1 |
|
} |
111
|
1 |
|
} |
112
|
|
|
|
113
|
1 |
|
return apply_filters( 'ppp_supported_post_types', $post_types ); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* Returns an array of the allowed post types |
118
|
|
|
* |
119
|
|
|
* @since 2.2.3 |
120
|
|
|
* @return array Array of post types PPP should work for |
121
|
|
|
*/ |
122
|
|
|
function ppp_allowed_post_types() { |
123
|
1 |
|
$ppp_options = get_option( 'ppp_options' ); |
124
|
|
|
|
125
|
1 |
|
return apply_filters( 'ppp_schedule_share_post_types', array_keys( $ppp_options['post_types'] ) ); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* Given a Post ID and Post object, should we try and save the metabox content |
130
|
|
|
* @param int $post_id The Post ID being saved |
131
|
|
|
* @param object $post WP_Post object of the post being saved |
132
|
|
|
* @return bool Wether to save the metabox or not |
133
|
|
|
*/ |
134
|
|
|
function ppp_should_save( $post_id, $post ) { |
135
|
|
|
$ret = true; |
136
|
|
|
|
137
|
|
|
if ( empty( $_POST ) ) { |
138
|
|
|
$ret = false; |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
if ( wp_is_post_revision( $post_id ) ) { |
142
|
|
|
$ret = false; |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
global $ppp_options; |
146
|
|
|
if ( !isset( $ppp_options['post_types'] ) || !is_array( $ppp_options['post_types'] ) || !array_key_exists( $post->post_type, $ppp_options['post_types'] ) ) { |
147
|
|
|
$ret = false; |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
return apply_filters( 'ppp_should_save', $ret, $post ); |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* Verifies our directory exists, and it's protected |
155
|
|
|
* |
156
|
|
|
* @since 2.2 |
157
|
|
|
* @return void |
158
|
|
|
*/ |
159
|
|
|
function ppp_set_uploads_dir() { |
160
|
|
|
$upload_path = ppp_get_upload_path(); |
161
|
|
|
|
162
|
|
|
if ( false === get_transient( 'ppp_check_protection_files' ) ) { |
163
|
|
|
|
164
|
|
|
// Make sure the /ppp folder is created |
165
|
|
|
wp_mkdir_p( $upload_path ); |
166
|
|
|
|
167
|
|
|
// Prevent directory browsing and direct access to all files |
168
|
|
|
$rules = "Options -Indexes\n"; |
169
|
|
|
$rules .= "deny from all\n"; |
170
|
|
|
|
171
|
|
|
$htaccess_exists = file_exists( $upload_path . '/.htaccess' ); |
172
|
|
|
|
173
|
|
|
if ( $htaccess_exists ) { |
174
|
|
|
$contents = @file_get_contents( $upload_path . '/.htaccess' ); |
175
|
|
|
if ( $contents !== $rules || ! $contents ) { |
176
|
|
|
// Update the .htaccess rules if they don't match |
177
|
|
|
@file_put_contents( $upload_path . '/.htaccess', $rules ); |
|
|
|
|
178
|
|
|
} |
179
|
|
|
} elseif( wp_is_writable( $upload_path ) ) { |
180
|
|
|
// Create the file if it doesn't exist |
181
|
|
|
@file_put_contents( $upload_path . '/.htaccess', $rules ); |
|
|
|
|
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
// Top level blank index.php |
185
|
|
|
if ( ! file_exists( $upload_path . '/index.php' ) && wp_is_writable( $upload_path ) ) { |
186
|
|
|
@file_put_contents( $upload_path . '/index.php', '<?php' . PHP_EOL . '// Silence is golden.' ); |
|
|
|
|
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
// Check for the files once per day |
190
|
|
|
set_transient( 'ppp_check_protection_files', true, 3600 * 24 ); |
191
|
|
|
} |
192
|
|
|
} |
193
|
|
|
add_action( 'admin_init', 'ppp_set_uploads_dir' ); |
194
|
|
|
|
195
|
|
|
/** |
196
|
|
|
* The location of where we store our files for Local tokens |
197
|
|
|
* |
198
|
|
|
* @since 2.2 |
199
|
|
|
* @return string The path to the /ppp folder in the uploads dir |
200
|
|
|
*/ |
201
|
|
|
function ppp_get_upload_path() { |
202
|
1 |
|
$wp_upload_dir = wp_upload_dir(); |
203
|
1 |
|
return $wp_upload_dir['basedir'] . '/ppp'; |
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
/** |
207
|
|
|
* Get's the array of completed upgrade actions |
208
|
|
|
* |
209
|
|
|
* @since 2.3 |
210
|
|
|
* @return array The array of completed upgrades |
211
|
|
|
*/ |
212
|
|
|
function ppp_get_completed_upgrades() { |
213
|
|
|
|
214
|
|
|
$completed_upgrades = get_option( 'ppp_completed_upgrades' ); |
215
|
|
|
|
216
|
|
|
if ( false === $completed_upgrades ) { |
217
|
|
|
$completed_upgrades = array(); |
218
|
|
|
} |
219
|
|
|
|
220
|
|
|
return $completed_upgrades; |
221
|
|
|
|
222
|
|
|
} |
223
|
|
|
|
224
|
|
|
/** |
225
|
|
|
* Check if the upgrade routine has been run for a specific action |
226
|
|
|
* |
227
|
|
|
* @since 2.3 |
228
|
|
|
* @param string $upgrade_action The upgrade action to check completion for |
229
|
|
|
* @return bool If the action has been added to the copmleted actions array |
230
|
|
|
*/ |
231
|
|
|
function ppp_has_upgrade_completed( $upgrade_action = '' ) { |
232
|
|
|
|
233
|
|
|
if ( empty( $upgrade_action ) ) { |
234
|
|
|
return false; |
235
|
|
|
} |
236
|
|
|
|
237
|
|
|
$completed_upgrades = ppp_get_completed_upgrades(); |
238
|
|
|
|
239
|
|
|
return in_array( $upgrade_action, $completed_upgrades ); |
240
|
|
|
|
241
|
|
|
} |
242
|
|
|
|
243
|
|
|
/** |
244
|
|
|
* Adds an upgrade action to the completed upgrades array |
245
|
|
|
* |
246
|
|
|
* @since 2.3 |
247
|
|
|
* @param string $upgrade_action The action to add to the copmleted upgrades array |
248
|
|
|
* @return bool If the function was successfully added |
249
|
|
|
*/ |
250
|
|
|
function ppp_set_upgrade_complete( $upgrade_action = '' ) { |
251
|
|
|
|
252
|
|
|
if ( empty( $upgrade_action ) ) { |
253
|
|
|
return false; |
254
|
|
|
} |
255
|
|
|
|
256
|
|
|
$completed_upgrades = ppp_get_completed_upgrades(); |
257
|
|
|
$completed_upgrades[] = $upgrade_action; |
258
|
|
|
|
259
|
|
|
// Remove any blanks, and only show uniques |
260
|
|
|
$completed_upgrades = array_unique( array_values( $completed_upgrades ) ); |
261
|
|
|
|
262
|
|
|
return update_option( 'ppp_completed_upgrades', $completed_upgrades ); |
263
|
|
|
} |
264
|
|
|
|
265
|
|
|
/** |
266
|
|
|
* Determines if the current site is a development or staging site. |
267
|
|
|
* |
268
|
|
|
* @return boolean |
269
|
|
|
*/ |
270
|
|
|
function ppp_is_dev_or_staging() { |
271
|
|
|
$is_local_url = false; |
272
|
|
|
|
273
|
|
|
// Trim it up |
274
|
|
|
$url = strtolower( trim( get_home_url( '/' ) ) ); |
275
|
|
|
|
276
|
|
|
// Need to get the host...so let's add the scheme so we can use parse_url |
277
|
|
|
if ( false === strpos( $url, 'http://' ) && false === strpos( $url, 'https://' ) ) { |
278
|
|
|
$url = 'http://' . $url; |
279
|
|
|
} |
280
|
|
|
|
281
|
|
|
$url_parts = parse_url( $url ); |
282
|
|
|
$host = ! empty( $url_parts['host'] ) ? $url_parts['host'] : false; |
283
|
|
|
|
284
|
|
|
if ( ! empty( $url ) && ! empty( $host ) ) { |
285
|
|
|
|
286
|
|
|
if ( false !== ip2long( $host ) ) { |
287
|
|
|
if ( ! filter_var( $host, FILTER_VALIDATE_IP, FILTER_FLAG_NO_PRIV_RANGE | FILTER_FLAG_NO_RES_RANGE ) ) { |
288
|
|
|
$is_local_url = true; |
289
|
|
|
} |
290
|
|
|
} else if ( 'localhost' === $host ) { |
291
|
|
|
$is_local_url = true; |
292
|
|
|
} |
293
|
|
|
|
294
|
|
|
$check_tlds = apply_filters( 'ppp_validate_tlds', true ); |
295
|
|
|
if ( $check_tlds ) { |
296
|
|
|
$tlds_to_check = apply_filters( 'ppp_url_tlds', array( |
297
|
|
|
'.dev', '.local', '.test' |
298
|
|
|
) ); |
299
|
|
|
|
300
|
|
|
foreach ( $tlds_to_check as $tld ) { |
301
|
|
|
if ( false !== strpos( $host, $tld ) ) { |
302
|
|
|
$is_local_url = true; |
303
|
|
|
continue; |
304
|
|
|
} |
305
|
|
|
} |
306
|
|
|
} |
307
|
|
|
|
308
|
|
|
if ( substr_count( $host, '.' ) > 1 ) { |
309
|
|
|
$subdomains_to_check = apply_filters( 'ppp_url_subdomains', array( |
310
|
|
|
'dev.', '*.staging.', |
311
|
|
|
) ); |
312
|
|
|
|
313
|
|
|
foreach ( $subdomains_to_check as $subdomain ) { |
314
|
|
|
|
315
|
|
|
$subdomain = str_replace( '.', '(.)', $subdomain ); |
316
|
|
|
$subdomain = str_replace( array( '*', '(.)' ), '(.*)', $subdomain ); |
317
|
|
|
|
318
|
|
|
if ( preg_match( '/^(' . $subdomain . ')/', $host ) ) { |
319
|
|
|
$is_local_url = true; |
320
|
|
|
continue; |
321
|
|
|
} |
322
|
|
|
} |
323
|
|
|
} |
324
|
|
|
} |
325
|
|
|
|
326
|
|
|
return apply_filters( 'ppp_is_local_url', $is_local_url, $url ); |
327
|
|
|
} |
328
|
|
|
|
If you suppress an error, we recommend checking for the error condition explicitly: