-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcodepotent-estimated-read-time.php
291 lines (238 loc) · 8.35 KB
/
codepotent-estimated-read-time.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
<?php
/**
* -----------------------------------------------------------------------------
* Plugin Name: Estimated Read Time
* Description: Displays estimated read-times for articles on the homepage, in search results, on category and tag pages, on date and author archive pages, and individual posts and pages.
* Version: 1.0.1
* Requires CP: 1.0
* Author: azurecurve
* Author URI: https://dev.azrcrv.co.uk/classicpress-plugins
* Plugin URI: https://dev.azrcrv.co.uk/classicpress-plugins
* Donate link: https://development.azurecurve.co.uk/support-development/
* Text Domain: estimated-read-time
* Domain Path: /languages
* License: GPLv2 or later
* License URI: http://www.gnu.org/licenses/gpl-2.0.html
* -----------------------------------------------------------------------------
* This is free software released under the terms of the General Public License,
* version 2, or later. It is distributed WITHOUT ANY WARRANTY; without even the
* implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. Full
* text of the license is available at https://www.gnu.org/licenses/gpl-2.0.txt.
* -----------------------------------------------------------------------------
* Copyright 2021, John Alarcon (Code Potent)
* Copyright 2021, Ian Grieve (azurecurve)
* -----------------------------------------------------------------------------
* Adopted by azurecurve, 06/01/2021
* -----------------------------------------------------------------------------
*/
// Declare the namespace.
namespace CodePotent\EstimatedReadTime;
// Prevent direct access.
if (!defined('ABSPATH')) {
die();
}
// include plugin menu
require_once(dirname( __FILE__).'/pluginmenu/menu.php');
add_action('admin_init', 'azrcrv_create_plugin_menu_ert');
// Include constants file.
require_once(plugin_dir_path(__FILE__).'includes/constants.php');
// Include update client.
require_once(plugin_dir_path(__FILE__).'classes/UpdateClient.class.php');
// Add action to load languages
add_action('plugins_loaded', __NAMESPACE__.'\\load_languages');
// add filter to add plugin action link
add_filter('plugin_action_links', __NAMESPACE__.'\\add_plugin_action_link', 10, 2);
// Add action to create admin menu
add_action('admin_menu', __NAMESPACE__.'\\create_admin_menu');
// Add action to save options
add_action('admin_post_azrcrv_ert_save_options', __NAMESPACE__.'\\save_options');
// Add action to process shortcodes.
add_shortcode('estimated-read-time', __NAMESPACE__.'\\process_shortcode');
/**
* Load language files.
*
* @author Ian Grieve
*
* @since 1.0.0
*
*/
function load_languages() {
$plugin_rel_path = basename(dirname(__FILE__)).'/languages';
load_plugin_textdomain('estimated-read-time', false, $plugin_rel_path);
}
/**
* Add action link on plugins page.
*
* @author Ian Grieve
*
* @since 1.0.0
*
*/
function add_plugin_action_link($links, $file){
static $this_plugin;
if (!$this_plugin){
$this_plugin = plugin_basename(__FILE__);
}
if ($file == $this_plugin){
$settings_link = '<a href="'.admin_url('admin.php?page=azrcrv-ert').'"><img src="'.plugins_url('/pluginmenu/images/logo.svg', __FILE__).'" style="padding-top: 2px; margin-right: -5px; height: 16px; width: 16px;" alt="azurecurve" />'.esc_html__('Settings' ,'estimated-read-time').'</a>';
array_unshift($links, $settings_link);
}
return $links;
}
/**
* Add to menu.
*
* @author Ian Grieve
*
* @since 1.0.0
*
*/
function create_admin_menu(){
add_submenu_page("azrcrv-plugin-menu"
,esc_html__("Estimated Read Time", "estimated-read-time")
,esc_html__("Estimated Read Time", "estimated-read-time")
,'manage_options'
,'azrcrv-ert'
,__NAMESPACE__.'\\display_options');
}
/**
* Get options with defaults.
*
* @author Ian Grieve
*
* @since 1.0.0
*
*/
function get_option_with_defaults($option_name){
$defaults = array(
'estimated-reading-speed' => 200,
);
$options = get_option($option_name, $defaults);
$options = wp_parse_args($options, $defaults);
return $options;
}
/**
* Display Settings page.
*
* @author Ian Grieve
*
* @since 1.0.0
*
*/
function display_options(){
if (!current_user_can('manage_options')){
wp_die(esc_html__('You do not have sufficient permissions to access this page.', 'estimated-read-time'));
}
global $wpdb;
// Retrieve plugin configuration options from database
$options = get_option_with_defaults('azrcrv-ert');
?>
<div id="azrcrv-ert-general" class="wrap">
<h1>
<?php
echo '<a href="https://development.azurecurve.co.uk/classicpress-plugins/"><img src="'.plugins_url('/pluginmenu/images/logo.svg', __FILE__).'" style="padding-right: 6px; height: 20px; width: 20px;" alt="azurecurve" /></a>';
esc_html_e(get_admin_page_title());
?>
</h1>
<?php
if(isset($_GET['settings-updated'])){ ?>
<div class="notice notice-success is-dismissible">
<p><strong><?php esc_html_e('Settings have been saved.', 'estimated-read-time'); ?></strong></p>
</div>
<?php } ?>
<div>
<form method="post" action="admin-post.php">
<fieldset>
<input type="hidden" name="action" value="azrcrv_ert_save_options" />
<input name="page_options" type="hidden" value="estimated-reading-speed" />
<!-- Adding security through hidden referrer field -->
<?php wp_nonce_field('azrcrv-ert', 'azrcrv-ert-nonce'); ?>
<table class="form-table">
<tr>
<th scope="row">
<label for="estimated-reading-speed">
<?php esc_html_e('Estimated Reading Speed', 'estimated-read-time'); ?>
</label>
</th>
<td>
<input name="estimated-reading-speed" type="number" step="5" min="10" id="estimated-reading-speed" value="<?php echo stripslashes($options['estimated-reading-speed']); ?>" class="small-text" />
<p class="description"><?php esc_html_e('A standard estimated read speed is approximately 200 words per minute; for more technical content, 100 would be common.', 'estimated-read-time'); ?></p>
</td>
</tr>
</table>
</fieldset>
<input type="submit" value="<?php esc_html_e('Save Changes', 'estimated-read-time'); ?>" class="button-primary"/>
</form>
</div>
</div>
<?php
}
/**
* Save settings.
*
* @author Ian Grieve
*
* @since 1.0.0
*
*/
function save_options(){
// Check that user has proper security level
if (!current_user_can('manage_options')){
wp_die(esc_html__('You do not have permissions to perform this action', 'estimated-read-time'));
}
// Check that nonce field created in configuration form is present
if (! empty($_POST) && check_admin_referer('azrcrv-ert', 'azrcrv-ert-nonce')){
// Retrieve original plugin options array
$options = get_option('azrcrv-ert');
$option_name = 'estimated-reading-speed';
if (isset($_POST[$option_name])){
$options[$option_name] = sanitize_text_field(intval($_POST[$option_name]));
}
// Store updated options array to database
update_option('azrcrv-ert', $options);
// Redirect the page to the configuration form that was processed
wp_redirect(add_query_arg('page', 'azrcrv-ert&settings-updated', admin_url('admin.php')));
exit;
}
}
/**
* Process shortcode.
*
* This function processes the estimated-read-time shortcode into HTML markup.
*
* @author John Alarcon
*
* @since 0.1.0
*
* @param array $atts Shortcode arguments.
* @return string Markup of shortcode output.
*/
function process_shortcode($atts) {
// Default values for when not passed in shortcode.
$defaults = [
'words' => 0,
];
// Replace any missing shortcode arguments with defaults.
$atts = shortcode_atts(
$defaults,
$atts,
'estimated-read-time');
// get plugin options with defaults
$options = get_option_with_defaults('azrcrv-ert');
// Reading speed; 200 wpm is average; use lower for more technical content.
$reading_speed = apply_filters('codepotent_estimated_read_time_speed', $options['estimated-reading-speed']);
// Read time in minutes; rounded up to nearest minute.
$reading_time = ceil( (int)$atts['words'] / $reading_speed );
// Return a translated string.
$markup = sprintf(
_n(
'%d minute read', // Singular text
'%d minute read', // Plural text
$reading_time, // Trigger for singular or plural
'estimated-read-time' // Text domain
),
$reading_time // The replacement value for %d in the text strings.
);
// Return estimated time string.
return $markup;
}