-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathload-webcomponents.php
186 lines (146 loc) · 4.68 KB
/
load-webcomponents.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
<?php
/**
* Plugin Name: Load Web Components
* Plugin URI: https://github.com/OnnoGeorg/wordpress-load-webcomponents
* Description: Loads web components triggered by shortcodes in posts and pages
* Author: Onno Gabriel
* Author URI: https://www.datacodedesign.de
* Text Domain: load_webcomponents
* Domain Path: /languages
* Version: 0.1.1
*
* @package Load_Webcomponents
*/
// If this file is accessed diretcly, then abort.
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
/***
* Plugin administration
*/
if ( is_admin() ) {
function load_webcomponents_load_textdomain() {
load_plugin_textdomain(
'load_webcomponents',
false,
basename( dirname( __FILE__ ) ) . '/languages'
);
}
add_action( 'init', 'load_webcomponents_load_textdomain' );
// Add settings link to plugins list
function load_webcomponents_settings_link( $links ) {
$settings_link = '<a href="options-general.php?page=load_webcomponents_settings_page">' . __( 'Settings' ) . '</a>';
array_push( $links, $settings_link );
return $links;
}
add_filter( 'plugin_action_links_' . plugin_basename( __FILE__ ), 'load_webcomponents_settings_link' );
// Admin settings page
require_once( dirname( __FILE__ ) . '/load-webcomponents_admin.php' );
// Return (no web components in the amdin area needed)
return;
}
/***
* Dummy shortcode addition.
* The short codes is not processed the standard way within the content,
* but in the page header via wp_head hook.
*/
function load_webcomponents_dummy( $atts ) {}
add_shortcode( 'load-webcomponent', 'load_webcomponents_dummy' );
/***
* Add shortcode analysis and script loader to page header
*/
function load_webcomponents_scripts() {
global $post;
// Not a post? => return
if( ! is_a( $post, 'WP_Post' ) ) {
return false;
}
// Load settings
$options = get_option( 'load_webcomponents_settings' );
// Web components disabled? => return
if ( ! isset( $options['load_webcomponents_enable_loading'] )
|| false == $options['load_webcomponents_enable_loading'] ) {
return false;
}
// Content contains no shortcode tag? => return
if ( false === strpos( $post->post_content, '[' ) ) {
return false;
}
// Get URLs of all web components added by shortcodes
$urls = load_webcomponents_get_webcomponents_urls( $post->post_content, $options );
// Echo all found web component/script URLs to HTML header:
if ( is_array( $urls ) && count ( $urls ) > 0 ) {
foreach ( $urls as $url ) {
echo "<script async defer src='" . $url . "'></script>\n";
}
}
}
add_action('wp_head', 'load_webcomponents_scripts');
/***
* Extracts all URLs from [load-webcomponent] short codes within the content
*/
function load_webcomponents_get_webcomponents_urls( $content = '', $options = array() ) {
if ($content === '') {
return false;
}
preg_match_all( '/' . get_shortcode_regex() . '/', $content, $matches, PREG_SET_ORDER );
if ( empty( $matches ) ) {
return false;
}
$urls = array();
//Go through all found shortcodes
foreach ( $matches as $shortcode ) {
// Shortcode "load-webcomponent"?
if ( 'load-webcomponent' === $shortcode[2] ) {
$url = '';
$i = 3;
while ( isset( $shortcode[$i] ) && ! empty( $shortcode[$i] ) ) {
// URLs by name identifier
$url = load_webcomponents_extract_url_by_name( $shortcode[$i], $options );
// Not found? Try to extract URLs from src attributes
if ( ! $url
&& isset( $options['load_webcomponents_allow_src'] )
&& true == $options['load_webcomponents_allow_src']
) {
$url = load_webcomponents_extract_url_by_src( $shortcode[$i] );
}
if ( $url !== false && ! empty( $url ) ) {
array_push( $urls, $url );
}
$i++;
}
}
}
return $urls;
}
/***
* Extracts URL from short code attribute 'name="<identifier>"'
*/
function load_webcomponents_extract_url_by_name( $string = '', $options = array() ) {
// No name identifiers defined => return
if (! isset( $options['load_webcomponents_list'] ) || ! is_array( $options['load_webcomponents_list'] ) ) {
return;
}
// Find name attribute
preg_match('/name\=[\"\'](.*?)[\"\']/', $string, $match);
if ( ! empty( $match ) && ! empty( $match[1] ) ) {
$name = esc_html( $match[1] );
foreach( $options['load_webcomponents_list'] as $index => $item) {
if ( $name == $item['name'] ) {
return esc_url_raw( $item['url'] );
}
}
}
return '';
}
/***
* Extracts URL from short code attribute 'src="<url>"'
*/
function load_webcomponents_extract_url_by_src( $string = '' ) {
// Fine src attribute
preg_match('/src\=[\"\'](.*?)[\"\']/', $string, $match);
if ( ! empty( $match ) && ! empty( $match[1] ) ) {
return esc_url( $match[1] );
}
return '';
}