-
Notifications
You must be signed in to change notification settings - Fork 0
/
ajaxify-for-wordpress.php
197 lines (181 loc) · 7.36 KB
/
ajaxify-for-wordpress.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
<?php
/*
Plugin Name: Ajaxify For Wordpress
GitHub Plugin URI: https://github.com/lupetalo/ajaxify-for-wordpress
Version: 1.2
*/
// add main js file
add_action( 'wp_enqueue_scripts', function () {
wp_register_script( 'ajaxify', plugins_url( '/assets/ajaxify.js' , __FILE__ ) );
if (get_option('ajaxify_enabled')=="true"){
wp_enqueue_script( 'ajaxify' );
}
});
// add inline code to wp_head
add_action('wp_head', function (){
// handle blanks and get options
$requestDelay=get_option('requestDelay');
$refresh=get_option('refresh');
$elements=get_option('elements');
$pronto_beforeload=get_option('pronto_beforeload');
$pronto_render=get_option('pronto_render');
$pronto_load=get_option('pronto_load');
if (empty($elements)) $elements='body';
if (empty($requestDelay)) $requestDelay='0';
if (empty($refresh)) $refresh='false';
if (get_option('ajaxify_enabled')=="true"){?>
<script>
jQuery( document ).ready(function() {
console.log( "ready!" );
let ajaxify = new Ajaxify({
selector: 'a[href^="<?php echo site_url();?>"]',
elements: '<?php echo $elements;?>',
requestDelay : '<?php echo $requestDelay;?>',
refresh : '<?php echo $refresh;?>',
});
<?php if (!empty($pronto_beforeload)){?>
window.addEventListener('pronto.beforeload', function(event){
<?php echo $pronto_beforeload;?>
event.stopPropagation();
})
<?php }?>
<?php if (!empty($pronto_render)){?>
window.addEventListener('pronto.render', function(event){
<?php echo $pronto_render;?>
event.stopPropagation();
})
<?php }?>
<?php if (!empty($pronto_load)){?>
window.addEventListener('pronto.load', function(event){
<?php echo $pronto_load;?>
event.stopPropagation();
})
<?php }?>
});
window.addEventListener('pronto.render', function(event){
if (window.elementorFrontend && window.elementorFrontend!='undefined'){
window.elementorFrontend.init();
event.stopPropagation();
}
})
</script>
<?php }});
// register settings
add_action( 'admin_init', function () {
register_setting( 'ajaxify_for_wp', 'ajaxify_enabled', ['default'=>'false']);
register_setting( 'ajaxify_for_wp', 'elements', ['default'=>'#content,#sidebar,#wpadminbar']);
register_setting( 'ajaxify_for_wp', 'triggres', ['default'=>'a:not(.no-ajaxy)']);
register_setting( 'ajaxify_for_wp', 'requestDelay', ['default'=>'0']);
register_setting( 'ajaxify_for_wp', 'refresh', ['default'=>'false']);
register_setting( 'ajaxify_for_wp', 'pronto_beforeload', ['default'=>null]);
register_setting( 'ajaxify_for_wp', 'pronto_render', ['default'=>null]);
register_setting( 'ajaxify_for_wp', 'pronto_load', ['default'=>null]);
});
// register options page
add_action('admin_menu', function () {
add_options_page('Ajaxify for WordPress', 'Ajaxify for WP', 'manage_options', 'ajaxify_for_wp', 'ajaxify_for_wp_options_page');
});
function ajaxify_for_wp_options_page(){?>
<div class="wrap">
<?php screen_icon(); ?>
<h1>Ajaxify for WordPress</h1>
<hr>
<a href="https://4nf.org/" target="_blank">Visit Ajaxify website</a>
<div>
<p>Most websites have the following page structure:</p>
<ul>
<li>Header</li>
<li>Sidebar(s)</li>
<li>Content</li>
<li>Footer</li>
<li></li>
</ul>
<p>…where only few elements vary significantly.</p>
<p>In the next sections you’ll find out how to load the varying elements only with this plugin,</p>
<p>avoiding a full page refresh and the associated full round-trip and creating awesome page load time.<br>
When ajaxifying a site, quite a few things have to be taken into account which this plugin takes care of… So it’s an entire Ajax framework!</p>
<p>With Ajaxify you turn almost any standard website into a shiny new single page application (SPA) effortlessly in a few minutes.</p>
</div>
<hr>
<form method="post" action="options.php">
<?php settings_fields( 'ajaxify_for_wp' );?>
<h3 class="title">Main Settings</h3>
<a href="https://4nf.org/interface/#idselection" target="_blank">Reference</a>
<table class="form-table">
<tr valign="top">
<th scope="row"><label for="elements">Ajaxify For WordPress enabled</label></th>
<td>
<select name="ajaxify_enabled">
<option value="true" <?php selected(get_option('ajaxify_enabled'), "true"); ?>>Yes</option>
<option value="false" <?php selected(get_option('ajaxify_enabled'), "false"); ?>>No</option>
</select>
</td>
</tr>
<tr valign="top">
<th scope="row"><label for="elements">Elements to refresh</label></th>
<td>
<input class="regular-text" type="text" id="elements" name="elements" value="<?php echo get_option('elements');?>" />
<p class="description">Use CSS Selectors, for example: <code>#elementID</code> or <code>.elementClass</code><br>
separate elements with comma <code>,</code> for example: <i>#elementID,.elementClass,footer</i></p>
</td>
</tr>
<tr valign="top">
<th scope="row"><label for="triggres">Elements that trigger ajax</label></th>
<td>
<input class="regular-text" type="text" id="triggres" name="triggres" value="<?php echo get_option('triggres');?>" />
<p class="description">Use CSS Selectors, for example: <code>#elementID</code> or <code>.elementClass</code><br>
separate elements with comma <code>,</code> for example: <i>#elementID,.elementClass,footer</i></p>
</td>
</tr>
</table>
<h3 class="title">Ajaxify Options</h3>
<a href="https://4nf.org/interface/#options" target="_blank">Reference</a>
<table class="form-table">
<tr valign="top">
<th scope="row"><label for="refresh">Refresh</label></th>
<td>
<select name="refresh">
<option value="true" <?php selected(get_option('refresh'), "true"); ?>>Yes</option>
<option value="false" <?php selected(get_option('refresh'), "false"); ?>>No</option>
</select>
<p class="description">Refresh the page even if link clicked is current page</p>
</td>
</tr>
<tr valign="top">
<th scope="row"><label for="requestDelay">requestDelay</label></th>
<td>
<input class="regular-text" type="number" id="requestDelay" name="requestDelay" value="<?php echo get_option('requestDelay');?>" />
<p class="description">in msec - Delay of Pronto request</p>
</td>
</tr>
</table>
<h3 class="title">Event's</h3>
<a href="https://4nf.org/events/" target="_blank">Reference</a>
<table class="form-table">
<tr valign="top">
<th scope="row"><label for="pronto_beforeload"><code>pronto.beforeload</code></label></th>
<td>
<textarea name="pronto_beforeload" id="pronto_beforeload" class="large-text code" rows="5"><?php echo get_option('pronto_beforeload');?></textarea>
<p class="description">Fired before new Pronto request is loaded</p>
</td>
</tr>
<tr valign="top">
<th scope="row"><label for="pronto_render"><code>pronto.render</code></label></th>
<td>
<textarea name="pronto_render" id="pronto_render" class="large-text code" rows="5"><?php echo get_option('pronto_render');?></textarea>
<p class="description">Fired after new Pronto request is rendered. <b>This is where you would reinitialise JS elements</b></p>
</td>
</tr>
<tr valign="top">
<th scope="row"><label for="pronto_load"><code>pronto.load</code></label></th>
<td>
<textarea name="pronto_load" id="pronto_load" class="large-text code" rows="5"><?php echo get_option('pronto_load');?></textarea>
<p class="description">Fired after new Pronto request is loaded</p>
</td>
</tr>
</table>
<?php submit_button(); ?>
</form>
</div>
<?php
}