This repository was archived by the owner on Nov 21, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwpdtrt-anchorlinks.php
371 lines (341 loc) · 12.6 KB
/
wpdtrt-anchorlinks.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
<?php
/**
* DTRT Anchor Links
*
* @package WPDTRT_Anchorlinks
* @author Dan Smith
* @copyright 2018 Do The Right Thing
* @license GPL-2.0+
*
* @wordpress-plugin
* Plugin Name: DTRT Anchor Links
* Plugin URI: https://github.com/dotherightthing/wpdtrt-anchorlinks
* Description: Anchor links plugin.
* Version: 0.4.10
* Author: Dan Smith
* Author URI: https://profiles.wordpress.org/dotherightthingnz
* License: GPLv2 or later
* License URI: http://www.gnu.org/licenses/gpl-2.0.html
* Text Domain: wpdtrt-anchorlinks
* Domain Path: /languages
*/
/**
* Constants
* WordPress makes use of the following constants when determining the path to the content and plugin directories.
* These should not be used directly by plugins or themes, but are listed here for completeness.
* WP_CONTENT_DIR // no trailing slash, full paths only
* WP_CONTENT_URL // full url
* WP_PLUGIN_DIR // full path, no trailing slash
* WP_PLUGIN_URL // full url, no trailing slash
*
* WordPress provides several functions for easily determining where a given file or directory lives.
* Always use these functions in your plugins instead of hard-coding references to the wp-content directory
* or using the WordPress internal constants.
* plugins_url()
* plugin_dir_url()
* plugin_dir_path()
* plugin_basename()
*
* @see https://codex.wordpress.org/Determining_Plugin_and_Content_Directories#Constants
* @see https://codex.wordpress.org/Determining_Plugin_and_Content_Directories#Plugins
*/
if ( ! defined( 'WPDTRT_ANCHORLINKS_VERSION' ) ) {
/**
* Plugin version.
*
* WP provides get_plugin_data(), but it only works within WP Admin,
* so we define a constant instead.
*
* @see $plugin_data = get_plugin_data( __FILE__ ); $plugin_version = $plugin_data['Version'];
* @see https://wordpress.stackexchange.com/questions/18268/i-want-to-get-a-plugin-version-number-dynamically
*/
define( 'WPDTRT_ANCHORLINKS_VERSION', '0.4.10' );
}
if ( ! defined( 'WPDTRT_ANCHORLINKS_PATH' ) ) {
/**
* Plugin directory filesystem path.
*
* @param string $file
* @return The filesystem directory path (with trailing slash)
* @see https://developer.wordpress.org/reference/functions/plugin_dir_path/
* @see https://developer.wordpress.org/plugins/the-basics/best-practices/#prefix-everything
*/
define( 'WPDTRT_ANCHORLINKS_PATH', plugin_dir_path( __FILE__ ) );
}
if ( ! defined( 'WPDTRT_ANCHORLINKS_URL' ) ) {
/**
* Plugin directory URL path.
*
* @param string $file
* @return The URL (with trailing slash)
* @see https://codex.wordpress.org/Function_Reference/plugin_dir_url
* @see https://developer.wordpress.org/plugins/the-basics/best-practices/#prefix-everything
*/
define( 'WPDTRT_ANCHORLINKS_URL', plugin_dir_url( __FILE__ ) );
}
/**
* ===== Dependencies =====
*/
/**
* Determine the correct path to the PSR-4 autoloader.
*
* @see https://github.com/dotherightthing/wpdtrt-plugin-boilerplate/issues/51
*/
if ( ! defined( 'WPDTRT_PLUGIN_CHILD' ) ) {
define( 'WPDTRT_PLUGIN_CHILD', true );
}
/**
* Determine the correct path to the PSR-4 autoloader.
*
* @see https://github.com/dotherightthing/wpdtrt-plugin-boilerplate/issues/104
* @see https://github.com/dotherightthing/wpdtrt-plugin-boilerplate/wiki/Options:-Adding-WordPress-plugin-dependencies
*/
if ( defined( 'WPDTRT_ANCHORLINKS_TEST_DEPENDENCY' ) ) {
$project_root_path = realpath( __DIR__ . '/../../..' ) . '/';
} else {
$project_root_path = '';
}
require_once $project_root_path . 'vendor/autoload.php';
if ( is_admin() ) {
// This replaces the TGMPA autoloader
// @see dotherightthing/generator-wpdtrt-plugin-boilerplate#77
// @see dotherightthing/wpdtrt-plugin-boilerplate#136.
require_once $project_root_path . 'vendor/tgmpa/tgm-plugin-activation/class-tgm-plugin-activation.php';
}
// sub classes, not loaded via PSR-4.
// remove the includes you don't need, edit the files you do need.
require_once WPDTRT_ANCHORLINKS_PATH . 'src/class-wpdtrt-anchorlinks-plugin.php';
require_once WPDTRT_ANCHORLINKS_PATH . 'src/class-wpdtrt-anchorlinks-shortcode.php';
// log & trace helpers.
global $debug;
$debug = new DoTheRightThing\WPDebug\Debug();
/**
* ===== WordPress Integration =====
*
* Comment out the actions you don't need.
*
* Notes:
* Default priority is 10. A higher priority runs later.
* register_activation_hook() is run before any of the provided hooks.
*
* @see https://developer.wordpress.org/plugins/hooks/actions/#priority
* @see https://codex.wordpress.org/Function_Reference/register_activation_hook.
*/
register_activation_hook( dirname( __FILE__ ), 'wpdtrt_anchorlinks_helper_activate' );
add_action( 'init', 'wpdtrt_anchorlinks_plugin_init', 0 );
add_action( 'init', 'wpdtrt_anchorlinks_shortcode_init', 100 );
add_action( 'init', 'wpdtrt_anchorlinks_shortcode_heading_init', 100 );
register_deactivation_hook( dirname( __FILE__ ), 'wpdtrt_anchorlinks_helper_deactivate' );
/**
* ===== Plugin config =====
*/
/**
* Register functions to be run when the plugin is activated.
*
* @see https://codex.wordpress.org/Function_Reference/register_activation_hook
* @todo https://github.com/dotherightthing/wpdtrt-plugin-boilerplate/issues/128
* @see See also Plugin::helper_flush_rewrite_rules()
*/
function wpdtrt_anchorlinks_helper_activate() {
flush_rewrite_rules();
}
/**
* Register functions to be run when the plugin is deactivated.
* (WordPress 2.0+)
*
* @see https://codex.wordpress.org/Function_Reference/register_deactivation_hook
* @todo https://github.com/dotherightthing/wpdtrt-plugin-boilerplate/issues/128
* @see See also Plugin::helper_flush_rewrite_rules()
*/
function wpdtrt_anchorlinks_helper_deactivate() {
flush_rewrite_rules();
}
/**
* Plugin initialisaton
*
* We call init before widget_init so that the plugin object properties are available to it.
* If widget_init is not working when called via init with priority 1, try changing the priority of init to 0.
* init: Typically used by plugins to initialize. The current user is already authenticated by this time.
* widgets_init: Used to register sidebars. Fired at 'init' priority 1 (and so before 'init' actions with priority ≥ 1!)
*
* @see https://wp-mix.com/wordpress-widget_init-not-working/
* @see https://codex.wordpress.org/Plugin_API/Action_Reference
* @todo Add a constructor function to WPDTRT_Anchorlinks_Plugin, to explain the options array
*/
function wpdtrt_anchorlinks_plugin_init() {
// pass object reference between classes via global
// because the object does not exist until the WordPress init action has fired.
global $wpdtrt_anchorlinks_plugin;
/**
* Global options
*
* @see https://github.com/dotherightthing/wpdtrt-plugin-boilerplate/wiki/Options:-Adding-global-options Options: Adding global options
*/
$plugin_options = array(
'heading_level' => array(
'type' => 'select',
'label' => __( 'Heading level', 'wpdtrt-anchorlinks' ),
'options' => array(
'h1' => array(
'text' => 'H1',
),
'h2' => array(
'text' => 'H2',
),
'h3' => array(
'text' => 'H3',
),
'h4' => array(
'text' => 'H4',
),
'h5' => array(
'text' => 'H5',
),
'h6' => array(
'text' => 'H6',
),
),
'tip' => __( 'Heading level to convert into anchors', 'wpdtrt-anchorlinks' ),
'default' => 'h2',
),
);
/**
* Shortcode or Widget options
*
* @see https://github.com/dotherightthing/wpdtrt-plugin-boilerplate/wiki/Options:-Adding-shortcode-or-widget-options Options: Adding shortcode or widget options
*/
$instance_options = array(
'post_id' => array(
'type' => 'number',
'size' => 3,
'label' => __( 'Post ID', 'wpdtrt-anchorlinks' ),
'tip' => __( 'Used in unit tests', 'wpdtrt-anchorlinks' ),
'default' => 1,
),
'title_text' => array(
'type' => 'text',
'size' => 30,
'label' => __( 'Anchor list title text', 'wpdtrt-anchorlinks' ),
'tip' => __( 'e.g. Outline', 'wpdtrt-anchorlinks' ),
'default' => __( 'Outline', 'wpdtrt-anchorlinks' ),
),
'exclude_widgets_on_pages' => array(
'type' => 'text',
'size' => 30,
'label' => __( 'Exclude widgets from anchor links list on these pages', 'wpdtrt-anchorlinks' ),
'tip' => __( 'Comma separated list of page IDs', 'wpdtrt-anchorlinks' ),
),
'additional_html' => array(
'type' => 'text',
'size' => 30,
'label' => __( 'Additional HTML', 'wpdtrt-anchorlinks' ),
'tip' => __( 'Output an HTML string below the list', 'wpdtrt-anchorlinks' ),
),
'additional_from_sidebar_id_1' => array(
'type' => 'text',
'size' => 100,
'label' => __( 'Sidebar 1 ID', 'wpdtrt-anchorlinks' ),
'tip' => __( 'ID of a sidebar outside of the_content, which contains widgets with titles', 'wpdtrt-anchorlinks' ),
),
'additional_from_sidebar_order_1' => array(
'type' => 'number',
'size' => 3,
'label' => __( 'Sidebar 1 order', 'wpdtrt-anchorlinks' ),
'tip' => __( 'DOM order of sidebar relative to the_content (0)', 'wpdtrt-anchorlinks' ),
'default' => -1,
),
'extra_header_class' => array(
'type' => 'string',
'size' => 100,
'label' => __( 'Extra header class', 'wpdtrt-anchorlinks' ),
),
);
/**
* UI Messages
*/
$ui_messages = array(
'demo_data_description' => __( 'This demo was generated from the following data', 'wpdtrt-anchorlinks' ),
'demo_data_displayed_length' => __( '# results displayed', 'wpdtrt-anchorlinks' ),
'demo_data_length' => __( '# results', 'wpdtrt-anchorlinks' ),
'demo_data_title' => __( 'Demo data', 'wpdtrt-anchorlinks' ),
'demo_date_last_updated' => __( 'Data last updated', 'wpdtrt-anchorlinks' ),
'demo_sample_title' => __( 'Demo sample', 'wpdtrt-anchorlinks' ),
'demo_shortcode_title' => __( 'Demo shortcode', 'wpdtrt-anchorlinks' ),
'insufficient_permissions' => __( 'Sorry, you do not have sufficient permissions to access this page.', 'wpdtrt-anchorlinks' ),
'no_options_form_description' => __( 'There aren\'t currently any options.', 'wpdtrt-anchorlinks' ),
'noscript_warning' => __( 'Please enable JavaScript', 'wpdtrt-anchorlinks' ),
'options_form_description' => __( 'Please enter your preferences.', 'wpdtrt-anchorlinks' ),
'options_form_submit' => __( 'Save Changes', 'wpdtrt-anchorlinks' ),
'options_form_title' => __( 'General Settings', 'wpdtrt-anchorlinks' ),
'loading' => __( 'Loading latest data...', 'wpdtrt-anchorlinks' ),
'success' => __( 'settings successfully updated', 'wpdtrt-anchorlinks' ),
);
/**
* Array: demo_shortcode_params
*
* Demo shortcode.
*
* See:
* - <Settings page - Adding a demo shortcode: https://github.com/dotherightthing/wpdtrt-plugin-boilerplate/wiki/Settings-page:-Adding-a-demo-shortcode>
*/
$demo_shortcode_params = array();
/**
* Plugin configuration
*/
$wpdtrt_anchorlinks_plugin = new WPDTRT_Anchorlinks_Plugin(
array(
'path' => WPDTRT_ANCHORLINKS_PATH,
'url' => WPDTRT_ANCHORLINKS_URL,
'version' => WPDTRT_ANCHORLINKS_VERSION,
'prefix' => 'wpdtrt_anchorlinks',
'slug' => 'wpdtrt-anchorlinks',
'menu_title' => __( 'Anchor Links', 'wpdtrt-anchorlinks' ),
'settings_title' => __( 'Settings', 'wpdtrt-anchorlinks' ),
'developer_prefix' => 'DTRT',
'messages' => $ui_messages,
'plugin_options' => $plugin_options,
'instance_options' => $instance_options,
'demo_shortcode_params' => $demo_shortcode_params,
)
);
}
/**
* ===== Shortcode config =====
*/
/**
* Register Shortcode
*/
function wpdtrt_anchorlinks_shortcode_init() {
global $wpdtrt_anchorlinks_plugin;
$wpdtrt_anchorlinks_shortcode = new WPDTRT_Anchorlinks_Shortcode(
array(
'name' => 'wpdtrt_anchorlinks_shortcode',
'plugin' => $wpdtrt_anchorlinks_plugin,
'template' => 'anchorlinks',
'selected_instance_options' => array(
'post_id',
'title_text',
'exclude_widgets_on_pages',
'additional_html',
'additional_from_sidebar_id_1',
'additional_from_sidebar_order_1',
),
)
);
}
/**
* Register Shortcode
*/
function wpdtrt_anchorlinks_shortcode_heading_init() {
global $wpdtrt_anchorlinks_plugin;
$wpdtrt_anchorlinks_shortcode_heading = new WPDTRT_Anchorlinks_Shortcode(
array(
'name' => 'wpdtrt_anchorlinks_shortcode_heading',
'plugin' => $wpdtrt_anchorlinks_plugin,
'template' => 'heading',
'selected_instance_options' => array(
'extra_header_class',
),
)
);
}