| 1 |
<?php |
| 2 |
/* |
| 3 |
Plugin Name: WP Reading Progress |
| 4 |
Plugin URI: https://github.com/joerivanveen/wp-reading-progress |
| 5 |
Description: Light weight customizable reading progress bar. Great UX on longreads! Customize under Settings -> WP Reading Progress |
| 6 |
Version: 1.5.3 |
| 7 |
Author: Joeri van Veen |
| 8 |
Author URI: https://wp-developer.eu |
| 9 |
License: GPLv3 |
| 10 |
Text Domain: wp-reading-progress |
| 11 |
Domain Path: /languages/ |
| 12 |
*/ |
| 13 |
defined('ABSPATH') or die(); |
| 14 |
// This is plugin nr. 6 by Ruige hond. It identifies as: ruigehond006. |
| 15 |
const RUIGEHOND006_VERSION = '1.5.3'; |
| 16 |
// Register hooks for plugin management, functions are at the bottom of this file. |
| 17 |
register_activation_hook(__FILE__, 'ruigehond006_install'); |
| 18 |
register_deactivation_hook(__FILE__, 'ruigehond006_deactivate'); |
| 19 |
register_uninstall_hook(__FILE__, 'ruigehond006_uninstall'); |
| 20 |
// Startup the plugin |
| 21 |
add_action('init', 'ruigehond006_run'); |
| 22 |
add_action('wp', 'ruigehond006_localize'); |
| 23 |
/** |
| 24 |
* the actual plugin on the frontend |
| 25 |
*/ |
| 26 |
function ruigehond006_run() |
| 27 |
{ |
| 28 |
if (is_admin()) { |
| 29 |
load_plugin_textdomain('wp-reading-progress', null, dirname(plugin_basename(__FILE__)) . '/languages/'); |
| 30 |
wp_enqueue_style('wp-color-picker'); |
| 31 |
wp_enqueue_script('wp-color-picker'); |
| 32 |
wp_enqueue_script('ruigehond006_admin_javascript', plugin_dir_url(__FILE__) . 'admin.min.js', 'wp-color-picker', RUIGEHOND006_VERSION, true); |
| 33 |
add_action('admin_init', 'ruigehond006_settings'); |
| 34 |
add_action('admin_menu', 'ruigehond006_menuitem'); |
| 35 |
add_filter('plugin_action_links_' . plugin_basename(__FILE__), 'ruigehond006_settingslink'); // settings link on plugins page |
| 36 |
add_action('add_meta_boxes', 'ruigehond006_meta_box_add'); // in the box the user can activate the bar for a single post |
| 37 |
add_action('save_post', 'ruigehond006_meta_box_save'); |
| 38 |
} else { |
| 39 |
wp_enqueue_script('ruigehond006_javascript', plugin_dir_url(__FILE__) . 'wp-reading-progress.min.js', false, RUIGEHOND006_VERSION); |
| 40 |
} |
| 41 |
} |
| 42 |
|
| 43 |
function ruigehond006_localize() |
| 44 |
{ |
| 45 |
if (!is_admin()) { |
| 46 |
$post_identifier = null; |
| 47 |
// check if we're using the progress bar here |
| 48 |
$option = get_option('ruigehond006'); |
| 49 |
$post_id = get_the_ID(); |
| 50 |
if (is_singular()) { |
| 51 |
if ((isset($option['post_types']) && in_array(get_post_type($post_id), $option['post_types'])) |
| 52 |
or 'yes' === get_post_meta($post_id, '_ruigehond006_show', true)) { |
| 53 |
if (isset($option['include_comments'])) { |
| 54 |
$post_identifier = 'body'; |
| 55 |
} else { |
| 56 |
$post_identifier = '.' . implode('.', get_post_class('', $post_id)); |
| 57 |
} |
| 58 |
} |
| 59 |
} elseif (isset($option['archives']) && isset($option['post_types']) && in_array(get_post_type(), $option['post_types'])) { |
| 60 |
$post_identifier = 'body'; |
| 61 |
} |
| 62 |
if (null !== $post_identifier) { |
| 63 |
wp_localize_script('ruigehond006_javascript', 'ruigehond006_c', array_merge( |
| 64 |
$option, array( |
| 65 |
'post_identifier' => $post_identifier, |
| 66 |
'post_id' => $post_id, |
| 67 |
) |
| 68 |
)); |
| 69 |
} |
| 70 |
if (!isset($option['no_css'])) add_action('wp_head', 'ruigehond006_stylesheet'); |
| 71 |
} |
| 72 |
} |
| 73 |
|
| 74 |
function ruigehond006_stylesheet() |
| 75 |
{ |
| 76 |
echo '<style>#ruigehond006_wrap{z-index:10001;position:fixed;display:block;left:0;width:100%;margin:0;overflow:visible}#ruigehond006_inner{position:absolute;height:0;width:inherit;background-color:rgba(255,255,255,.2);-webkit-transition:height .4s;transition:height .4s}html[dir=rtl] #ruigehond006_wrap{text-align:right}#ruigehond006_bar{width:0;height:100%;background-color:transparent}</style>'; |
| 77 |
} |
| 78 |
|
| 79 |
// meta box exposes setting to display reading progress for an individual post |
| 80 |
// https://developer.wordpress.org/reference/functions/add_meta_box/ |
| 81 |
function ruigehond006_meta_box_add($post_type = null) |
| 82 |
{ |
| 83 |
if (!get_the_ID()) { |
| 84 |
return; |
| 85 |
} |
| 86 |
$option = get_option('ruigehond006'); |
| 87 |
if (isset($option['post_types']) and in_array($post_type, $option['post_types'])) { |
| 88 |
return; // you can't set this if the bar is displayed by default on this post type |
| 89 |
} |
| 90 |
add_meta_box( // WP function. |
| 91 |
'ruigehond006', // Unique ID |
| 92 |
'WP Reading Progress', // Box title |
| 93 |
'ruigehond006_meta_box', // Content callback, must be of type callable |
| 94 |
$post_type, // Post type |
| 95 |
'normal', |
| 96 |
'low', |
| 97 |
array('option' => $option) |
| 98 |
); |
| 99 |
} |
| 100 |
|
| 101 |
function ruigehond006_meta_box($post, $obj) |
| 102 |
{ |
| 103 |
//$option = $obj['args']['option']; // not used at this moment |
| 104 |
wp_nonce_field('ruigehond006_save', 'ruigehond006_nonce'); |
| 105 |
echo '<input type="checkbox" id="ruigehond006_checkbox" name="ruigehond006_show"'; |
| 106 |
if ('yes' === get_post_meta($post->ID, '_ruigehond006_show', true)) echo ' checked="checked"'; |
| 107 |
echo '/> <label for="ruigehond006_checkbox">'; |
| 108 |
echo __('display reading progress bar', 'wp-reading-progress'); |
| 109 |
echo '</label>'; |
| 110 |
} |
| 111 |
|
| 112 |
function ruigehond006_meta_box_save($post_id) |
| 113 |
{ |
| 114 |
if (!isset($_POST['ruigehond006_nonce']) || !wp_verify_nonce($_POST['ruigehond006_nonce'], 'ruigehond006_save')) |
| 115 |
return; |
| 116 |
if (!current_user_can('edit_post', $post_id)) |
| 117 |
return; |
| 118 |
if (isset($_POST['ruigehond006_show'])) { |
| 119 |
add_post_meta($post_id, '_ruigehond006_show', 'yes', true); |
| 120 |
} else { |
| 121 |
delete_post_meta($post_id, '_ruigehond006_show'); |
| 122 |
} |
| 123 |
} |
| 124 |
|
| 125 |
/** |
| 126 |
* manage global settings |
| 127 |
*/ |
| 128 |
function ruigehond006_settings() |
| 129 |
{ |
| 130 |
/** |
| 131 |
* register a new setting, call this function for each setting |
| 132 |
* Arguments: (Array) |
| 133 |
* - group, the same as in settings_fields, for security / nonce etc. |
| 134 |
* - the name of the options |
| 135 |
* - the function that will validate the options, valid options are automatically saved by WP |
| 136 |
*/ |
| 137 |
register_setting('ruigehond006', 'ruigehond006', 'ruigehond006_settings_validate'); |
| 138 |
// register a new section in the page |
| 139 |
add_settings_section( |
| 140 |
'progress_bar_settings', // section id |
| 141 |
__('Set your options', 'wp-reading-progress'), // title |
| 142 |
function () { |
| 143 |
echo '<p>'; |
| 144 |
echo __('This plugin displays a reading progress bar on your selected post types.', 'wp-reading-progress'); |
| 145 |
echo ' '; |
| 146 |
echo __('When it does not find a single article, it uses the whole page to calculate reading progress.', 'wp-reading-progress'); |
| 147 |
echo '<br/>'; |
| 148 |
echo __('For post types which are switched off in the settings, you can activate the bar per post in the post-edit screen.', 'wp-reading-progress'); |
| 149 |
echo '<br/>'; |
| 150 |
echo __('Please use valid input or the bar might not display.', 'wp-reading-progress'); |
| 151 |
echo '</p>'; |
| 152 |
}, //callback |
| 153 |
'ruigehond006' // page |
| 154 |
); |
| 155 |
if (false === ($option = get_option('ruigehond006'))) { |
| 156 |
ruigehond006_add_defaults(); |
| 157 |
$option = get_option('ruigehond006'); |
| 158 |
} |
| 159 |
ruigehond006_add_settings_field( |
| 160 |
'bar_attach', |
| 161 |
'text', |
| 162 |
__('Stick the bar to this element', 'wp-reading-progress'), // title |
| 163 |
$option, |
| 164 |
// #translators: two links are inserted that set the value accordingly, 'top' and 'bottom' |
| 165 |
sprintf(__('Use %s or %s, or any VALID selector of a fixed element where the bar can be appended to, e.g. a sticky menu.', 'wp-reading-progress'), |
| 166 |
'<a>top</a>', '<a>bottom</a>') . ' ' . __('Multiple selectors can be separated by commas, the bar will be attached to the first one visible.', 'wp-reading-progress') |
| 167 |
); |
| 168 |
ruigehond006_add_settings_field( |
| 169 |
'stick_relative', |
| 170 |
'checkbox', |
| 171 |
__('How to stick', 'wp-reading-progress'), |
| 172 |
$option, |
| 173 |
__('If the bar is too wide, try relative positioning by checking this box, or attach it to another element.', 'wp-reading-progress') |
| 174 |
); |
| 175 |
ruigehond006_add_settings_field( |
| 176 |
'bar_color', |
| 177 |
'color', |
| 178 |
__('Color of the progress bar', 'wp-reading-progress'), // title |
| 179 |
$option |
| 180 |
); |
| 181 |
// ruigehond006_add_settings_field( |
| 182 |
// 'bar_color_dark_mode', |
| 183 |
// 'color', |
| 184 |
// __('Color when in dark mode', 'wp-reading-progress'), // title |
| 185 |
// $option, |
| 186 |
// sprintf(__('Depends on a certain class added to the body or html container, including one of the following strings: %s', 'wp-reading-progress'), '*dark-mode*, *night-mode*') |
| 187 |
// ); |
| 188 |
ruigehond006_add_settings_field( |
| 189 |
'bar_height', |
| 190 |
'text-short', |
| 191 |
__('Progress bar thickness', 'wp-reading-progress'), // title |
| 192 |
$option, |
| 193 |
sprintf(__('Thickness based on screen height is recommended, e.g. %s. But you can also use pixels, e.g. %s.', 'wp-reading-progress'), '<a>.5vh</a>', '<a>6px</a>') |
| 194 |
); |
| 195 |
ruigehond006_add_settings_field( |
| 196 |
'aria_label', |
| 197 |
'text', |
| 198 |
__('Aria label', 'wp-reading-progress'), // title |
| 199 |
$option, |
| 200 |
__('Explain the purpose of this reading bar to screenreaders', 'wp-reading-progress') |
| 201 |
); |
| 202 |
// ruigehond006_add_settings_field( |
| 203 |
// 'ert_speed', |
| 204 |
// 'text-short', |
| 205 |
// __('Reading speed', 'wp-reading-progress'), // title |
| 206 |
// $option, |
| 207 |
// __('Average reading speed in words per minute, integers only. Used to estimate reading time. Leave empty for no ERT. Usual is something between 200 and 300.', 'wp-reading-progress') |
| 208 |
// ); |
| 209 |
ruigehond006_add_settings_field( |
| 210 |
'mark_it_zero', |
| 211 |
'checkbox', |
| 212 |
__('Make bar start at 0%', 'wp-reading-progress'), |
| 213 |
$option, |
| 214 |
__('Yes please', 'wp-reading-progress') |
| 215 |
); |
| 216 |
ruigehond006_add_settings_field( |
| 217 |
'include_comments', |
| 218 |
'checkbox', |
| 219 |
__('On single post page', 'wp-reading-progress'), |
| 220 |
$option, |
| 221 |
__('use whole page to calculate reading progress', 'wp-reading-progress') |
| 222 |
); |
| 223 |
add_settings_field( |
| 224 |
'ruigehond006_post_types', |
| 225 |
// #TRANSLATORS: this is followed by a list of the available post_types |
| 226 |
__('Show reading progress on', 'wp-reading-progress'), |
| 227 |
function ($args) { |
| 228 |
$post_types = []; |
| 229 |
if (isset($args['option']['post_types'])) { |
| 230 |
$post_types = $args['option']['post_types']; |
| 231 |
} |
| 232 |
foreach (get_post_types(array('public' => true)) as $post_type) { |
| 233 |
echo "<label><input type=\"checkbox\" name=\"ruigehond006[post_types][]\" value=\"$post_type\""; |
| 234 |
if (in_array($post_type, $post_types)) { |
| 235 |
echo ' checked="checked"'; |
| 236 |
} |
| 237 |
echo "/>$post_type</label><br/>"; |
| 238 |
} |
| 239 |
echo '<div class="ruigehond006 explanation"><em>'; |
| 240 |
echo __('For unchecked post types you can enable the reading progress bar per post on the post edit page.', 'wp-reading-progress'); |
| 241 |
echo '</em></div>'; |
| 242 |
}, |
| 243 |
'ruigehond006', |
| 244 |
'progress_bar_settings', |
| 245 |
['option' => $option] // args |
| 246 |
); |
| 247 |
ruigehond006_add_settings_field( |
| 248 |
'archives', |
| 249 |
'checkbox', |
| 250 |
__('And on their archives', 'wp-reading-progress'), |
| 251 |
$option |
| 252 |
); |
| 253 |
ruigehond006_add_settings_field( |
| 254 |
'no_css', |
| 255 |
'checkbox', |
| 256 |
'No css', |
| 257 |
$option, |
| 258 |
__('necessary css for the reading bar is included elsewhere', 'wp-reading-progress') |
| 259 |
); |
| 260 |
} |
| 261 |
|
| 262 |
function ruigehond006_add_settings_field($name, $type, $title, $option, $explanation = null) |
| 263 |
{ |
| 264 |
add_settings_field( |
| 265 |
'ruigehond006_' . $name, |
| 266 |
$title, |
| 267 |
function ($args) { |
| 268 |
switch ($args['type']) { |
| 269 |
case 'checkbox': |
| 270 |
echo '<label><input type="checkbox" name="ruigehond006['; |
| 271 |
echo $args['name']; |
| 272 |
echo ']"'; |
| 273 |
if (isset($args['value']) and $args['value']) { |
| 274 |
echo ' checked="checked"'; |
| 275 |
} |
| 276 |
echo '/> '; |
| 277 |
echo $args['explanation']; |
| 278 |
echo '</label>'; |
| 279 |
|
| 280 |
return; |
| 281 |
case 'color': |
| 282 |
echo '<input type="text" class="ruigehond006_colorpicker" name="ruigehond006['; |
| 283 |
echo $args['name']; |
| 284 |
echo ']" value="'; |
| 285 |
echo $args['value']; |
| 286 |
echo '"/>'; |
| 287 |
break; |
| 288 |
default: // regular input |
| 289 |
$value = isset($args['value']) ? $args['value'] : ''; |
| 290 |
echo '<input type="text" name="ruigehond006['; |
| 291 |
echo $args['name']; |
| 292 |
echo ']" value="'; |
| 293 |
echo htmlentities($value); |
| 294 |
if ('text-short' !== $args['type']) echo '" class="regular-text'; |
| 295 |
echo '"/>'; |
| 296 |
} |
| 297 |
if (isset($args['explanation'])) { |
| 298 |
echo '<div class="ruigehond006 explanation"><em>'; |
| 299 |
echo $args['explanation']; |
| 300 |
echo '</em></div>'; |
| 301 |
} |
| 302 |
}, |
| 303 |
'ruigehond006', |
| 304 |
'progress_bar_settings', |
| 305 |
array( |
| 306 |
'name' => $name, |
| 307 |
'type' => $type, |
| 308 |
'value' => isset($option[$name]) ? $option[$name] : null, |
| 309 |
'explanation' => $explanation, |
| 310 |
) // args |
| 311 |
); |
| 312 |
} |
| 313 |
|
| 314 |
function ruigehond006_settingspage() |
| 315 |
{ |
| 316 |
if (!current_user_can('manage_options')) { |
| 317 |
return; |
| 318 |
} |
| 319 |
echo '<div class="wrap"><h1>'; |
| 320 |
echo esc_html(get_admin_page_title()); |
| 321 |
echo '</h1><form action="options.php" method="post">'; |
| 322 |
// output security fields for the registered setting |
| 323 |
settings_fields('ruigehond006'); |
| 324 |
// output setting sections and their fields |
| 325 |
do_settings_sections('ruigehond006'); |
| 326 |
// output save settings button |
| 327 |
submit_button(__('Save Settings', 'wp-reading-progress')); |
| 328 |
echo '</form></div>'; |
| 329 |
} |
| 330 |
|
| 331 |
function ruigehond006_settingslink($links) |
| 332 |
{ |
| 333 |
$url = get_admin_url(); |
| 334 |
$txt = __('Settings', 'wp-reading-progress'); |
| 335 |
$settings_link = "<a href=\"{$url}options-general.php?page=wp-reading-progress\">$txt</a>"; |
| 336 |
array_unshift($links, $settings_link); |
| 337 |
|
| 338 |
return $links; |
| 339 |
} |
| 340 |
|
| 341 |
function ruigehond006_menuitem() |
| 342 |
{ |
| 343 |
add_options_page( |
| 344 |
'WP Reading Progress', |
| 345 |
'WP Reading Progress', |
| 346 |
'manage_options', |
| 347 |
'wp-reading-progress', |
| 348 |
'ruigehond006_settingspage' |
| 349 |
); |
| 350 |
} |
| 351 |
|
| 352 |
/** |
| 353 |
* plugin management functions |
| 354 |
*/ |
| 355 |
function ruigehond006_add_defaults() |
| 356 |
{ |
| 357 |
add_option('ruigehond006', array( |
| 358 |
'bar_attach' => 'top', |
| 359 |
'bar_color' => '#f1592a', |
| 360 |
'bar_height' => '.5vh', |
| 361 |
'post_types' => array('post'), |
| 362 |
), null, true); |
| 363 |
} |
| 364 |
|
| 365 |
function ruigehond006_install() |
| 366 |
{ |
| 367 |
if (!get_option('ruigehond006')) { // insert default settings: |
| 368 |
ruigehond006_add_defaults(); |
| 369 |
} |
| 370 |
} |
| 371 |
|
| 372 |
function ruigehond006_deactivate() |
| 373 |
{ |
| 374 |
// nothing to do really |
| 375 |
} |
| 376 |
|
| 377 |
function ruigehond006_uninstall() |
| 378 |
{ |
| 379 |
// remove settings |
| 380 |
delete_option('ruigehond006'); |
| 381 |
delete_option('ruigehond006_upgraded_1.2.4'); |
| 382 |
// remove the post_meta entries |
| 383 |
delete_post_meta_by_key('_ruigehond006_show'); |
| 384 |
} |