-
Notifications
You must be signed in to change notification settings - Fork 2
/
press-this-extended.php
465 lines (409 loc) · 16.1 KB
/
press-this-extended.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
<?php
/**
* Press This Extended
*
* @package BJGK\Press_this_extended
* @version 1.1.0
* @author Brandon Kraft <public@brandonkraft.com>
* @copyright Copyright (c) 2015, Brandon Kraft
* @link https://www.brandonkraft.com/press-this-extended/
* @license GPL-2.0+
*
* @wordpress-plugin
* Plugin Name: Press This Extended
* Plugin URI: https://www.brandonkraft.com/press-this-extended/
* Description: Provides options for extending and modifying the Press This feature (WP 4.2+)
* Version: 1.1.0
* Author: Brandon Kraft
* Author URI: https://www.brandonkraft.com
* License: GPL-2.0+
* License URI: http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
* Text Domain: press-this-extended
* Domain Path: /languages
*/
/*
* This program is free software; you can redistribute it and/or modify it under the terms of the GNU
* General Public License version 2, as published by the Free Software Foundation. You may NOT assume
* that you can use any other version of the GPL.
*
* This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
* even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
*
*/
class Press_This_Extended {
/**
* Sets the cruise control at 88 MPH. In other words, let's fire the engines
*
* Selectively adding hooks based on need so we don't cross streams resulting in every molecule in our bodies exploding.
*
* @since 1.0.0
* @return void
*/
function __construct() {
global $pagenow;
add_action( 'admin_init', array( $this, 'load_translations' ) , 1 ); // These filters are generally used within wp-admin.
add_action( 'admin_init', array( $this, 'add_settings' ) );
add_action( 'admin_init', array( $this, 'legacy_conversion') );
add_action( 'admin_init', array( $this, 'execute' ) );
add_action( 'admin_head-press-this.php', array( $this, 'mobile_app_meta' ) );
add_action( 'load-options-writing.php', array( $this, 'help_tab' ) );
add_filter( 'plugin_action_links_' . plugin_basename(__FILE__), array( $this, 'action_links' ) );
if ( 'admin-ajax.php' == $pagenow ) { // These hooks are the only one used exclusively within the ajax context.
add_action( 'admin_init', array( $this, 'execute_ajax' ) );
}
}
/**
* Load the textdomain / translations for the plugin.
*
* @since 1.0.0
* @return void
*/
function load_translations() {
$domain = 'press-this-extended';
$locale = apply_filters( 'plugin_locale', get_locale(), $domain );
load_textdomain( $domain, trailingslashit( WP_LANG_DIR ) . $domain . '/' . $domain . '-' . $locale . '.mo' );
load_plugin_textdomain( $domain, false, basename( dirname( __FILE__ ) ) . '/languages' );
}
/**
* Add settings field to the the Settings->Writing page.
*
* @return void
* @since 1.0.0
**/
function add_settings() {
$slug = 'press-this-extended';
add_settings_section( $slug, 'Press This', null, 'writing');
add_settings_field( $slug . '-media', __( 'Content Grabbing', $slug ), array( $this, 'setting_media' ), 'writing', $slug );
register_setting( 'writing', $slug . '-media', 'intval' );
add_filter( 'default_option_'. $slug . '-media', '__return_true' );
add_settings_field( $slug . '-text', null, array( $this, 'setting_text' ), 'writing', $slug );
register_setting( 'writing', $slug . '-text', 'intval' );
add_filter( 'default_option_' . $slug . '-text', '__return_true' );
add_settings_field( $slug . '-blockquote', __('Blockquote Formatting', $slug), array( $this, 'setting_blockquote' ), 'writing', $slug );
register_setting( 'writing', $slug . '-blockquote', 'wp_kses_post' );
add_filter( 'default_option_' . $slug . '-blockquote', array( $this, 'default_blockquote' ) ); // When WP is 5.3+, use anonymous function.
add_settings_field( $slug . '-citation', __('Citation Formatting', $slug), array( $this, 'setting_citation' ), 'writing', $slug );
register_setting( 'writing', $slug . '-citation', 'wp_kses_post' );
add_filter( 'default_option_' . $slug . '-citation', array( $this, 'default_citation' ) ); // When WP is 5.3+, use anonymous function.
add_settings_field( $slug . '-parent', __('Redirection', $slug), array( $this, 'setting_parent' ), 'writing', $slug );
register_setting( 'writing', $slug . '-parent', 'intval' );
add_filter( 'default_option_' . $slug . '-parent', '__return_false' );
add_settings_field( $slug . '-save-publish', __( 'Upon Publishing...', 'press-this-extended' ), array( $this, 'setting_save_publish' ), 'writing', $slug );
register_setting( 'writing', $slug . '-save-publish', array( $this, 'save_sanitize' ) );
add_filter( 'default_option_' . $slug . '-save-publish', array( $this, 'default_publish' ) );
add_settings_field( $slug . '-save-draft', __( 'Upon Saving a Draft...', 'press-this-extended' ), array( $this, 'setting_save_draft' ), 'writing', $slug );
register_setting( 'writing', $slug . '-save-draft', array( $this, 'save_sanitize' ) );
add_filter( 'default_option_' . $slug . '-save-draft', array( $this, 'default_save' ) );
}
/**
* Santizes setting options for save_publish and save_draft settings.
*
* @return string Either 'pt', 'permalink', 'editor'. Defaults to 'editor' if validation fails.
* @since 1.0.0
**/
function save_sanitize( $value ) {
if ( $value != 'permalink' && $value != 'pt' ) {
$value = 'editor';
}
return $value;
}
/**
* Handles deprecated settings.
*
* Checks for deprecated settings and converts/delete as needed.
*
* @return void
* @since 1.0.0 'legacy' setting conversion.
* @since 1.1.0 'editor' setting deletion.
**/
function legacy_conversion() {
$legacy = get_option( 'press-this-extended-legacy', 'nothing' ); // sets a default value if setting is not present in the DB. Can't use false since that is a valid setting state in the db.
if ( $legacy == 1 ) {
$citation = '<p>via <a href="%1$s">%2$s</a></p>';
update_option( 'press-this-extended-media', false );
update_option( 'press-this-extended-text', false );
update_option( 'press-this-extended-citation', $citation );
delete_option( 'press-this-extended-legacy' );
}
elseif ( $legacy == false ) {
delete_option( 'press-this-extended-legacy' );
}
if ( get_option( 'press-this-extended-editor' ) ) {
delete_option( 'press-this-extended-editor' );
}
}
/**
* Returns the default blockquote wrapper original to Press This
*
* @return string Default blockquote wrapping with %1$s being the variable for the quote.
* @since 1.0.0
**/
function default_blockquote() {
return '<blockquote>%1$s</blockquote>';
}
/**
* Returns the default citation or "source" wrapper original to Press This.
*
* The default's use of "Source" is translated natively in WordPress Core, so no plugin language domain is indicated
*
* @return string Default source wrapping with %1$s representing the URL and %2$s the pressed page's title.
* @since 1.0.0
**/
function default_citation() {
$html = '<p>' . _x( 'Source:', 'Used in Press This to indicate where the content comes from.' ) .
' <em><a href="%1$s">%2$s</a></em></p>';
return $html;
}
/**
* Returns the default value for saving a draft.
*
* @return string Default draft redirect location.
* @since 1.0.0
**/
function default_save() {
return 'pt';
}
/**
* Returns the default value for publishing a post.
*
* @return string Default publish redirect location.
* @since 1.0.0
**/
function default_publish() {
return 'permalink';
}
/**
* Echos the Media Discovery setting form field
*
* @return void
* @since 1.0.0
**/
function setting_media(){
$html = '<input type="checkbox" id="press-this-extended-media" name="press-this-extended-media" value="1" ' . checked(1, get_option('press-this-extended-media'), false) . '/>';
$html .= '<label for="press-this-extended-media"> ' . __( 'Should Press This suggest media to add to a new post?', 'press-this-extended' ) . '</label>';
echo $html;
}
/**
* Echos the Text Discovery setting form field
*
* @return void
* @since 1.0.0
**/
function setting_text(){
$html = '<input type="checkbox" id="press-this-extended-text" name="press-this-extended-text" value="1" ' . checked(1, get_option('press-this-extended-text'), false) . '/>';
$html .= '<label for="press-this-extended-text"> ' . __( "Should Press This try to suggest a quote if you haven't preselected text?", 'press-this-extended' ) . '</label>';
echo $html;
}
/**
* Echos the Blockquote wrapper setting form field
*
* @return void
* @since 1.0.0
**/
function setting_blockquote(){
$html = '<textarea id="press-this-extended-blockquote" name="press-this-extended-blockquote" class="large-text ltr">';
$html .= esc_textarea( get_option('press-this-extended-blockquote'));
$html .= '</textarea>';
$html .= '<p class="description">' . __( 'Use %1$s as a placeholder for the blockquote.', 'press-this-extended' ) .'</p>';
echo $html;
}
/**
* Echos the Citation wrapper setting form field
*
* @return void
* @since 1.0.0
**/
function setting_citation(){
$html = '<textarea id="press-this-extended-citation" name="press-this-extended-citation" class="large-text ltr">';
$html .= esc_textarea( get_option('press-this-extended-citation'));
$html .= '</textarea>';
$html .= '<p class="description">' . __( 'Use %1$s and %2$s as a placeholders for the page URL and title, respectively.', 'press-this-extended' ) .'</p>';
echo $html;
}
/**
* Echos HTML for the Parent Redirection option setting form field.
*
* @return void
* @since 1.0.0
**/
function setting_parent() {
$html = '<input type="checkbox" id="press-this-extended-parent" name="press-this-extended-parent" value="1" ' . checked(1, get_option('press-this-extended-parent'), false) . '/>';
$html .= '<label for="press-this-extended-parent"> ' . __( 'Upon publishing or saving a draft, close the Press This popup and redirect the original tab.', 'press-this-extended' ) . '</label>';
echo $html;
}
/**
* Echos HTML for the selecting redirect when publishing a post.
*
* @return void
* @since 1.0.0
**/
function setting_save_publish() {
$options = array(
'permalink' => __( 'Published Post', 'press-this-extended' ),
'editor' => __( 'Standard Editor' ),
);
$extra_text = __( 'After publishing a post, you will be redirected to this location.', 'press-this-extended' );
$this->settings_select( 'press-this-extended-save-publish', $options, $extra_text );
}
/**
* Echos HTML for the selecting redirect when saving a draft.
*
* @return void
* @since 1.0.0
**/
function setting_save_draft() {
$options = array(
'pt' => __( 'Remain in the Press This editor', 'press-this-extended' ),
'editor' => __( 'Standard Editor' ),
);
$extra_text = __( 'After saving a draft, you will be redirected to this location.', 'press-this-extended' );
$this->settings_select( 'press-this-extended-save-draft', $options, $extra_text );
}
/**
* Echos HTML dropdown selection forms.
*
* @return void
* @since 1.0.0
**/
function settings_select( $name, $values, $extra_text = '' ) {
if ( empty( $name ) || empty( $values ) || ! is_array( $values ) ) {
return;
}
$option = get_option( $name );
?>
<fieldset>
<select name="<?php echo esc_attr( $name ); ?>" id="<?php echo esc_attr( $name ); ?>">
<?php foreach ( $values as $key => $value ) : ?>
<option value="<?php echo esc_attr( $key ); ?>" <?php selected( $key, $option ); ?>>
<?php echo esc_html( $value ); ?>
</option>
<?php endforeach; ?>
</select>
<?php if ( ! empty( $extra_text ) ) : ?>
<p class="description"><?php echo esc_html( $extra_text ); ?></p>
<?php endif; ?>
</fieldset>
<?php
}
/**
* Used when filtering the default html from Press This based on the various options set in Press This Extended.
*
* @return array $html {
* @type string $quote Blockquote wrapping with placeholder %1$s for the quoted text.
* @type string $link Citation wrapping with placeholders %1$s and %2$s for Pressed URL and page title, respectively.
* }
* @since 1.0.0
**/
function execute_html( $html, $data ){
$text_discovery = get_option( 'press-this-extended-text' );
$html = array(
'quote' => get_option( 'press-this-extended-blockquote'),
'link' => get_option( 'press-this-extended-citation'),
);
if ( $text_discovery == false && ! isset( $data['s'] ) ) {
$html['quote'] = '';
}
/* Leaving this in for now for future reference.
if ( $legacy ) {
if ( isset( $data['s'] ) ){
$html = array(
'quote' => '<p>%1$s</p>',
'link' => '<p>via <a href="%1$s">%2$s</a></p>',
);
}
else {
$html = array(
'quote' => '',
'link' => '<a href="%1$s">%2$s</a>',
);
}
}*/
return $html;
}
/**
* Execute the settings by adding the filters specified by the various settings.
*
* @return void
* @since 1.0.0
**/
function execute() {
$text_discovery = get_option( 'press-this-extended-text' );
$media_discovery = get_option( 'press-this-extended-media' );
$redirect_parent = get_option( 'press-this-extended-parent' );
if ( $media_discovery == false ) {
add_filter( 'enable_press_this_media_discovery', '__return_false' );
}
add_filter( 'press_this_suggested_html', array( $this, 'execute_html' ), 10, 2 );
if ( $redirect_parent ) {
add_filter( 'press_this_redirect_in_parent', '__return_true' );
}
}
/**
* Execute filters only used in the AJAX context.
*
* @return void
* @since 1.0.0
**/
function execute_ajax() {
add_filter( 'press_this_save_redirect', array( $this, 'redirect_publish'), 10, 3 );
}
/**
* Determines the redirected URL upon publishing a Press This post.
*
* This could be expanded in the future to do other fun things.
*
* @return URL of redirectoin
* @since 1.0.0
*/
function redirect_publish( $redirect, $post_id, $post_status ) {
$save_publish = get_option( 'press-this-extended-save-publish' );
$save_draft = get_option( 'press-this-extended-save-draft' );
if ( ( 'publish' == $post_status && $save_publish == 'editor' ) || 'publish' != $post_status && $save_draft == 'editor' ) {
$redirect = get_edit_post_link( $post_id, 'raw' );
}
return $redirect; // otherwise, it is using the default action within WordPress. We want their option in case we need to flesh this out later.
}
/**
* Adds contextual help for Press This Extended options.
*
* @return void
* @since 1.0.0
**/
function help_tab() {
get_current_screen()->add_help_tab( array(
'id' => 'options-press-this-extended',
'title' => __('Press This'),
'content' => '<p>' . __( 'These options allow you to customize the Press This bookmarklet to do some cool stuff.' ) . '</p>' .
'<p>' . __( 'Content Grabbing refers to Press This suggesting media and text for you to add in your pressed post. By default, Press This will suggest a blockfrom from the page\'s description unless it appears to be autogenerated.', 'press-this-extended' ) . '</p>' .
'<p>' . __( 'Blockquote and Citation Formatting allows you to adjust the default HTML template used when pressing a post.', 'press-this-extended' ) . '</p>' .
'<p>' . __( 'Redirection, Upon Publishing..., and Upon Saving A Draft... adjusts what happens when you save your content from Press This.', 'press-this-extended' ) . '</p>',
)
);
}
/**
* Adds Settings link to plugin table.
*
* @return array $links
*
* @since 1.0.0
**/
function action_links( $links ) {
$links[] = '<a href="'. get_admin_url(null, 'options-writing.php') .'">Settings</a>';
return $links;
}
/**
* Makes Press This into a webapp for iOS and Chrome for Android.
*
* @return void
*
* @since 1.1.0
**/
function mobile_app_meta(){
$html = '<meta name="apple-mobile-web-app-capable" content="yes">';
$html .= '<meta name="apple-mobile-web-app-status-bar-style" content="black">';
$html .= '<meta name="mobile-web-app-capable" content="yes">'; // Chrome for Android.
echo $html;
}
}
// Giddy up.
$Press_This_Extended = new Press_This_Extended;