forked from metafizzy/infinite-scroll
-
Notifications
You must be signed in to change notification settings - Fork 0
/
wp_infinite_scroll.php
454 lines (373 loc) · 16 KB
/
wp_infinite_scroll.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
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
<?php
/*
Plugin Name: Infinite Scroll
Version: 1.5.100504
Plugin URI: http://www.infinite-scroll.com
Description: Automatically loads the next page of posts into the bottom of the initial page.
Author: dirkhaim & Paul Irish
Author URI: http://www.infinite-scroll.com
License : http://creativecommons.org/licenses/GPL/2.0/
BUGS:
- javascript insertion doesnt work on themes: qwiilm!, craving4green, Lush, no limits, stripedplus
TODO:
- Allow to customize the speed of the fadeOut effect (or maybe even choose a different effect like hide..)
- deal with the IE6 + opacity + cleartype + <em> issue.
- What error handling do we need?
- Mention div#infscr-loading so users can customize look more.
- Check if you're in a table and thus you can't add divs.
- fix this: http://capacity.electronest.com/2009/01/13/fixing-url-path/
*/
// constants for enables/disabled
define('infscr_enabled' , 'enabled');
define('infscr_disabled' , 'disabled');
define('infscr_maint' , 'disabledforadmins');
define('infscr_config' , 'enabledforadmins');
// options keys constants
define('key_infscr_state' , 'infscr_state');
define('key_infscr_js_calls' , 'infscr_js_calls');
define('key_infscr_image' , 'infscr_image');
define('key_infscr_text' , 'infscr_text');
define('key_infscr_donetext' , 'infscr_donetext');
define('key_infscr_content_selector' , 'infscr_content_selector');
define('key_infscr_nav_selector' , 'infscr_nav_selector');
define('key_infscr_post_selector' , 'infscr_post_selector');
define('key_infscr_next_selector' , 'infscr_next_selector');
// defaults
define('infscr_state_default' , infscr_config);
define('infscr_js_calls_default' , '');
$image_path = get_option('siteurl').'/wp-content/plugins/infinite-scroll'.'/ajax-loader.gif';
define('infscr_image_default' , $image_path);
define('infscr_text_default' , '<em>Loading the next set of posts...</em>');
define('infscr_donetext_default' , '<em>Congratulations, you\'ve reached the end of the internet.</em>');
define('infscr_content_selector_default' , '#content');
define('infscr_post_selector_default' , '#content div.post');
define('infscr_nav_selector_default' , 'div.navigation');
define('infscr_next_selector_default', 'div.navigation a:first');
// add options
add_option(key_infscr_state , infscr_state_default , 'If InfiniteScroll is turned on, off, or in maintenance');
add_option(key_infscr_js_calls , infscr_js_calls_default , 'Javascript to execute when new content loads in');
add_option(key_infscr_image , infscr_image_default , 'Loading image');
add_option(key_infscr_text , infscr_text_default , 'Loading text');
add_option(key_infscr_donetext , infscr_donetext_default , 'Completed text');
add_option(key_infscr_content_selector , infscr_content_selector_default , 'Content Div css selector');
add_option(key_infscr_nav_selector , infscr_nav_selector_default , 'Navigation Div css selector');
add_option(key_infscr_post_selector , infscr_post_selector_default , 'Post Div css selector');
add_option(key_infscr_next_selector , infscr_next_selector_default , 'Next page Anchor css selector');
// adding actions
add_action('wp_footer' , 'wp_inf_scroll_add');
add_action('admin_menu' , 'add_wp_inf_scroll_options_page');
/*
// used because wordpress doesnt like to tell us for sure what the homepage is.
// removed because it doesnt quite work..
function is_frontpage()
{
global $post;
$id = $post->ID;
$show_on_front = get_option('show_on_front');
$page_on_front = get_option('page_on_front');
if ($show_on_front == 'page' && $page_on_front == $id ) { return true; }
else { return false; }
}
*/
if ( get_option(key_infscr_state) == infscr_state_default && !isset($_POST['submit']) ) {
function setup_warning() {
echo "
<div id='infinitescroll-warning' class='updated fade'><p><strong>".__('Infinite Scroll is almost ready.')."</strong> ".sprintf(__('Please <a href="%1$s">review the configuration and set the state to enabled for all users</a>.'), "options-general.php?page=wp_infinite_scroll.php")."</p></div>
";
}
add_action('admin_notices', 'setup_warning');
return;
}
function add_wp_inf_scroll_options_page()
{
global $wpdb;
add_options_page('Infinite Scroll Options', 'Infinite Scroll', 8, basename(__FILE__), 'wp_inf_scroll_options_page');
}
function wp_inf_scroll_options_page()
{
// if postback, store options
if (isset($_POST['info_update']))
{
check_admin_referer();
// update state
$infscr_state = $_POST[key_infscr_state];
if ($infscr_state != infscr_enabled && $infscr_state != infscr_disabled && $infscr_state != infscr_maint && $infscr_state != infscr_config)
$infscr_state = infscr_state_default;
update_option(key_infscr_state, $infscr_state);
// update js calls field
$infscr_js_calls = $_POST[key_infscr_js_calls];
update_option(key_infscr_js_calls, $infscr_js_calls);
// update image
$infscr_image = $_POST[key_infscr_image];
update_option(key_infscr_image, $infscr_image);
// update text
$infscr_text = $_POST[key_infscr_text];
update_option(key_infscr_text, $infscr_text);
// update done text
$infscr_donetext = $_POST[key_infscr_donetext];
update_option(key_infscr_donetext, $infscr_donetext);
// update content selector
$content_selector = $_POST[key_infscr_content_selector];
update_option(key_infscr_content_selector, $content_selector);
// update the navigation selector
$navigation_selector = $_POST[key_infscr_nav_selector];
update_option(key_infscr_nav_selector, $navigation_selector);
// update the post selector
$post_selector = $_POST[key_infscr_post_selector];
update_option(key_infscr_post_selector, $post_selector);
// update the next selector
$next_selector = $_POST[key_infscr_next_selector];
update_option(key_infscr_next_selector, $next_selector);
// update notification
echo "<div class='updated'><p><strong>Infinite Scroll options updated</strong></p></div>";
}
// output the options page
?>
<form method="post" action="options-general.php?page=<?php echo basename(__FILE__); ?>">
<div class="wrap">
<?php if (get_option(key_infscr_state) == infscr_disabled) { ?>
<div style="margin:10px auto; border:3px #f00 solid; background-color: #fdd; color: #000; padding: 10px; text-align: center;">
Infinite Scroll plugin is <strong>disabled</strong>.
</div>
<?php } ?>
<?php if ( false && get_option(key_infscr_state) != infscr_disabled && get_option(key_infscr_js_calls) == '') { // disabled for now?>
<div style="margin:10px auto; border:1px #f00 solid; background-color:#fdd; color:#000; padding:10px; text-align:center;">
No Javascript calls will be made after the content is added. This might cause errors in newly added content.
</div>
<?php } ?>
<style type="text/css">
table.infscroll-opttable { width: 100%;}
table.infscroll-opttable td, table.infscroll-opttable th { vertical-align: top; padding: 9px 4px; }
table.infscroll-opttable th { padding-top: 13px; text-align: right;}
table.infscroll-opttable td p { margin: 0;}
table.infscroll-opttable dl { font-size: 90%; color: #666; margin-top: 5px; }
table.infscroll-opttable dd { margin-bottom: 0 }
</style>
<h2>Infinite Scroll Options</h2>
<p>All CSS selectors are found with the jQuery javascript library. See the <a href="http://docs.jquery.com/Selectors">jQuery CSS Selector documentation</a> for an overview of all possibilities. Single-quotes are not allowed—only double-quotes may be used.
<table class="editform infscroll-opttable" cellspacing="0" >
<tbody>
<tr>
<th width="30%" >
<label for="<?php echo key_infscr_state; ?>">Infinite Scroll state is:</label>
</th>
<td>
<?php
echo "<select name='".key_infscr_state."' id='".key_infscr_state."'>\n";
echo "<option value='".infscr_enabled."'";
if (get_option(key_infscr_state) == infscr_enabled)
echo "selected='selected'";
echo ">Enabled for all users</option>\n";
echo "<option value='".infscr_disabled."'";
if (get_option(key_infscr_state) == infscr_disabled)
echo "selected='selected'";
echo ">Disabled for all users</option>\n";
echo "<option value='".infscr_config."'";
if (get_option(key_infscr_state) == infscr_config)
echo "selected='selected'";
echo ">Enabled for admins only</option>\n";
echo "<option value='".infscr_maint."'";
if (get_option(key_infscr_state) == infscr_maint)
echo "selected='selected'";
echo ">Disabled for admins only</option>\n";
echo "</select>";
?>
</td>
<td width="50%">
"Enabled for admins only" will enable the plugin code only for logged-in administrators—visitors will not be affected while you configure the plugin. "Disabled for admins only" is useful for administrators when customizing the blog—infinite scroll will be disabled for them, but still enabled for any visitors.
</td>
</tr>
<tr>
<th>
<label for="<?php echo key_infscr_content_selector; ?>">Content CSS Selector:</label>
</th>
<td>
<?php
echo "<input name='".key_infscr_content_selector."' id='".key_infscr_content_selector."' value='".stripslashes(get_option(key_infscr_content_selector))."' size='30' type='text'>\n";
?>
</td>
<td>
<p>The selector of the content div on the main page.</p>
</td>
</tr>
<tr>
<th >
<label for="<?php echo key_infscr_post_selector; ?>">Post CSS Selector:</label>
</th>
<td>
<?php
echo "<input name='".key_infscr_post_selector."' id='".key_infscr_post_selector."' value='".stripslashes(get_option(key_infscr_post_selector))."' size='30' type='text'>\n";
?>
</td>
<td>
<p>The selector of the post block.</p>
<dl>
<dt>Examples:</dt>
<dd>#content > *</dd>
<dd>#content div.post</dd>
<dd>div.primary div.entry</dd>
</dl>
</td>
</tr>
<tr>
<th>
<label for="<?php echo key_infscr_nav_selector; ?>">Navigation Links CSS Selector:</label>
</th>
<td>
<?php
echo "<input name='".key_infscr_nav_selector."' id='".key_infscr_nav_selector."' value='".stripslashes(get_option(key_infscr_nav_selector))."' size='30' type='text'>\n";
?>
</td>
<td>
<p>The selector of the navigation div (the one that includes the next and previous links).</p>
</td>
</tr>
<tr>
<th>
<label for="<?php echo key_infscr_next_selector; ?>">Previous posts CSS Selector:</label>
</th>
<td>
<?php
echo "<input name='".key_infscr_next_selector."' id='".key_infscr_next_selector."' value='".stripslashes(get_option(key_infscr_next_selector))."' size='30' type='text'>\n";
?>
</td>
<td>
<p>The selector of the previous posts (next page) A tag.</p>
<dl>
<dt>Examples:</dt>
<dd>div.navigation a:first</dd>
<dd>div.navigation a:contains(Previous)</dd>
</dl>
</td>
</tr>
<tr>
<th>
<label for="<?php echo key_infscr_js_calls; ?>">Javascript to be called after the next posts are fetched:</label>
</th>
<td>
<?php
echo "<textarea name='".key_infscr_js_calls."' rows='2' style='width: 95%;'>\n";
echo stripslashes(get_option(key_infscr_js_calls));
echo "</textarea>\n";
?>
</td>
<td>
<p>Any functions that are applied to the post contents on page load will need to be executed when the new content comes in.</p>
</td>
</tr>
<tr>
<th>
<label for="<?php echo key_infscr_image; ?>">Loading image:</label>
</th>
<td>
<?php
echo "<input name='".key_infscr_image."' id='".key_infscr_image."' value='".stripslashes(get_option(key_infscr_image))."' size='30' type='text'>\n";
?>
</td>
<td>
<p>URL of image that will be displayed while content is being loaded. Visit <a href="http://www.ajaxload.info" target="_blank">www.ajaxload.info</a> to customize your own loading spinner.</p>
</td>
</tr>
<tr>
<th>
<label for="<?php echo key_infscr_text; ?>">Loading text:</label>
</th>
<td>
<?php
echo "<input name='".key_infscr_text."' id='".key_infscr_text."' value='".stripslashes(get_option(key_infscr_text))."' size='30' type='text'>\n";
?>
</td>
<td>
<p>Text will be displayed while content is being loaded. <small><acronym>HTML</acrynom> allowed.</small></p>
</td>
</tr>
<tr>
<th>
<label for="<?php echo key_infscr_donetext; ?>">"You've reached the end" text:</label>
</th>
<td>
<?php
echo '<input name="'.key_infscr_donetext.'" id="'.key_infscr_donetext.'" value="'.stripslashes(get_option(key_infscr_donetext)).'" size="30" type="text">';
?>
</td>
<td>
<p>Text will be displayed when all entries have already been retrieved. The plugin will show this message, fade it out, and cease working. <small><acronym>HTML</acrynom> allowed.</small></p>
</td>
</tr>
</tbody>
</table>
<p class="submit">
<input type='submit' name='info_update' value='Update Options' />
</p>
</div>
</form>
<?php
}
function wp_inf_scroll_add()
{
global $user_level;
if (get_option(key_infscr_state) == infscr_disabled)
return;
if (is_page() || is_single() ) /* single posts/pages dont get it */
{
echo '<!-- Infinite-Scroll not added for this page (single post/page) -->';
return;
}
/*
if (! is_paged() ) non-paged dont get it
{
echo '<!-- Infinite-Scroll not added for this page (not paged) -->';
return;
}
*/
/* !is_home() || !is_paged() || paged (archive, tags, categories) and home do. */
if (get_option(key_infscr_state) == infscr_maint && $user_level >= 8)
{
echo '<!-- Infinite-Scroll not added for administrator (maintenance state) -->';
return;
}
if (get_option(key_infscr_state) == infscr_config && $user_level <= 8)
{
echo '<!-- Infinite-Scroll not added for visitors (configuration state) -->';
return;
}
$plugin_dir = get_option('siteurl').'/wp-content/plugins/infinite-scroll';
$js_calls = stripslashes(get_option(key_infscr_js_calls));
$loading_image = stripslashes(get_option(key_infscr_image));
$loading_text = stripslashes(get_option(key_infscr_text));
$donetext = stripslashes(get_option(key_infscr_donetext));
$content_selector = stripslashes(get_option(key_infscr_content_selector));
$navigation_selector = stripslashes(get_option(key_infscr_nav_selector));
$post_selector = stripslashes(get_option(key_infscr_post_selector));
$next_selector = stripslashes(get_option(key_infscr_next_selector));
if ($user_level >= 8) {$isAdmin = "true"; }else {$isAdmin = "false";}
$js_string = <<<EOT
<script type="text/javascript">
if (!(window.jQuery && jQuery.fn.jquery >= '1.2.6')){
document.write(unescape("%3Cscript src='http://ajax.googleapis.com/ajax/libs/jquery/1.4.1/jquery.min.js' type='text/javascript'%3E%3C/script%3E"));
window.INFSCR_jQ=true;
}
</script>
<script type="text/javascript" src="$plugin_dir/jquery.infinitescroll.min.js"></script>
<script type="text/javascript" >
(window.INFSCR_jQ ? jQuery.noConflict() : jQuery)(function($){
// Infinite Scroll jQuery+Wordpress plugin
$('$content_selector').infinitescroll({
debug : $isAdmin,
nextSelector : "$next_selector",
loadingImg : "$loading_image",
loadingText : "$loading_text",
donetext : "$donetext",
navSelector : "$navigation_selector",
contentSelector : "$content_selector",
itemSelector : "$post_selector"
},function(){
$js_calls
});
});
</script>
EOT;
echo $js_string;
return;
}
?>