forked from surrealroad/wp-onebox
-
Notifications
You must be signed in to change notification settings - Fork 0
/
onebox.php
409 lines (350 loc) · 15.8 KB
/
onebox.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
<?php
/*
Plugin Name: Onebox
Plugin URI: http://onebox.surrealroad.com
Description: Replaces a boring hyperlink with a lovely Facebook/Twitter-style box with additional information about the destination page
Version: 1.0
Author: Surreal Road Limited
Author URI: http://www.surrealroad.com
Text Domain: onebox
Domain Path: /languages
License: MIT
*/
/*
// debug
error_reporting(E_ALL);
ini_set('error_reporting', E_ALL);
ini_set('display_errors',1);
*/
// Make sure we don't expose any info if called directly
if ( !function_exists( 'add_action' ) ) {
echo 'Hi there! I\'m just a plugin, not much I can do when called directly.';
exit;
}
class OneboxPlugin {
//Version
static $version ='1.0';
static $sampleLink = "https://github.com/surrealroad/wp-onebox";
static $enableAffiliate = true; // if you change this, you may need to deactive/reactivate the plugin to see the changes
//Options and defaults
public function __construct() {
register_activation_hook(__FILE__,array(__CLASS__, 'install' ));
register_uninstall_hook(__FILE__,array( __CLASS__, 'uninstall'));
add_action('init', array($this, 'init'));
add_action('admin_init', array($this, 'admin_init'));
add_action('admin_init', array($this,'registerSettings'));
add_action('admin_menu', array($this,'pluginSettings'));
}
static function install(){
update_option("onebox_version",self::$version);
update_option("onebox_affiliate_links", self::$enableAffiliate);
add_option("onebox_template_html", '<div class="onebox-result {class}">
<div class="onebox-header-wrapper">
<div class="onebox-header">
<a href="{url}" target="_blank" rel="nofollow">{favicon}<span class="onebox-sitename">{sitename}</span></a> / <span class="onebox-title"><a href="{url}" target="_blank">{title}</a></span>
<span class="onebox-title-button">{title-button}</span>
</div>
</div>
<div class="onebox-body-wrapper">
<a href="{url}" target="_blank">{image}</a>
<div class="onebox-body">
<p class="onebox-description">{description} — <a href="{url}">Read More</a></p><p class="onebox-additional">{additional}</p>
</div>
</div>
<div class="onebox-footer-wrapper">
<div class="onebox-footer"><span class="onebox-footer-info">{footer}</span><span class="onebox-footer-button">{footer-button}</span></div>
</div>
<div class="onebox-clearfix"></div>
</div>');
add_option("onebox_enable_css", true);
add_option("onebox_enable_flat_css", true);
add_option("onebox_enable_dark_css", false);
add_option("onebox_selector", ".onebox-container");
add_option("onebox_enable_apc_cache", true);
add_option("onebox_github_apikey", '');
}
static function uninstall(){
delete_option('onebox_version');
delete_option('onebox_affiliate_links');
delete_option('onebox_template_html');
delete_option('onebox_enable_css');
delete_option('onebox_enable_flat_css');
delete_option('onebox_enable_dark_css');
delete_option('onebox_selector');
delete_option('onebox_enable_apc_cache');
delete_option('onebox_github_apikey');
}
public function init() {
//Allow translations
load_plugin_textdomain('onebox', false, basename(dirname(__FILE__)).'/languages');
add_action('wp_enqueue_scripts', array($this, 'enqueueScripts'));
add_action('wp_enqueue_scripts', array($this, 'enqueueStyles'));
add_filter('mce_css', array($this, 'enqueueTinyMCEStyles'));
add_action('admin_enqueue_scripts', array($this, 'enqueueAdminScripts'));
add_filter( 'query_vars', array($this, 'onebox_query_vars_filter') );
add_action( 'template_redirect', array($this, 'renderOnebox'), 1 );
$plugin = plugin_basename(__FILE__);
add_filter("plugin_action_links_$plugin", array($this, 'onebox_settings_link') );
add_filter( 'mce_buttons', array($this, 'registerTinyMCEButton') );
add_filter( "mce_external_plugins", array($this, 'registerTinyMCEButtonJS') );
}
public function admin_init() {
add_settings_section('onebox-template', __( 'Template Configuration', 'onebox' ), array($this, 'initTemplateSettings'), 'onebox');
add_settings_field('onebox-template-html', __( 'Template HTML', 'onebox' ), array($this, 'templateHTMLInput'), 'onebox', 'onebox-template');
add_settings_field('onebox-enable-css', __( 'Enable Styles', 'onebox' ), array($this, 'templateCSSInput'), 'onebox', 'onebox-template');
add_settings_field('onebox-enable-flat-css', __( 'Use Flat Theme', 'onebox' ), array($this, 'templateFlatCSSInput'), 'onebox', 'onebox-template');
add_settings_field('onebox-enable-dark-css', __( 'Use Dark Theme', 'onebox' ), array($this, 'templateDarkCSSInput'), 'onebox', 'onebox-template');
add_settings_field('onebox-selector', __( 'jQuery Selector for Oneboxes', 'onebox' ), array($this, 'templateSelectorInput'), 'onebox', 'onebox-template');
add_settings_section('onebox-features', __( 'Special Features', 'onebox' ), array($this, 'initFeatureSettings'), 'onebox');
add_settings_field('onebox-apc-cache', __( 'Enable APC Caching', 'onebox' ), array($this, 'apcCacheInput'), 'onebox', 'onebox-features');
add_settings_field('onebox-github-api', __( 'GitHub API Token', 'onebox' ), array($this, 'githubAPIInput'), 'onebox', 'onebox-features');
}
function registerSettings() {
register_setting('onebox', 'onebox_template_html');
register_setting('onebox', 'onebox_enable_css');
register_setting('onebox', 'onebox_enable_flat_css');
register_setting('onebox', 'onebox_enable_dark_css');
register_setting('onebox', 'onebox_selector');
register_setting('onebox', 'onebox_enable_apc_cache');
register_setting('onebox', 'onebox_github_apikey');
}
// Enqueue Javascript
function enqueueScripts() {
wp_enqueue_script(
'onebox',
plugins_url( '/js/onebox.min.js' , __FILE__ ),
array( 'jquery' )
);
// build settings to use in script http://ottopress.com/2010/passing-parameters-from-php-to-javascripts-in-plugins/
$params = array(
"renderURL" => site_url('/?onebox_render=1'),
"template" => wp_kses_post(get_option('onebox_template_html')),
"flat" => get_option('onebox_enable_flat_css'),
"dark" => get_option('onebox_enable_dark_css'),
"selector" => get_option('onebox_selector'),
);
wp_localize_script( 'onebox', 'OneboxParams', $params );
}
function enqueueAdminScripts(){
$this->enqueueScripts();
// for WordPress 4.0+, enable wp.mce api plugin
if(version_compare(get_bloginfo( "version" ), "4.0-beta", ">=")) wp_enqueue_script(
'onebox-mce-view',
plugins_url( '/js/onebox-mce-view.min.js' , __FILE__ ),
array( 'mce-view' ),
"1.0",
false
);
}
// Enqueue Static CSS
function enqueueStyles() {
wp_register_style( 'oneboxStylesheet', plugins_url( '/style/onebox.min.css' , __FILE__ ) );
if(get_option('onebox_enable_css')) wp_enqueue_style( 'oneboxStylesheet' );
}
function enqueueTinyMCEStyles($mce_css) {
if(get_option('onebox_enable_css')) {
if(!empty($mce_css)) $mce_css .= ',';
$mce_css .= plugins_url( '/style/onebox.min.css' , __FILE__ );
}
return $mce_css;
}
// add [onebox] shortcode
static function renderOneboxShortcode($atts) {
extract(shortcode_atts(array(
'url' => "",
'title' => "",
'description' => "",
), $atts));
if(!$url) return;
if($title) {
$data = ' data-title="'.esc_attr($title).'"';
} else {
$data = "";
$title = __( 'Link', 'onebox');
}
if($description) $data .= ' data-description="'.esc_attr($description).'"';
$link = '<a href="'.$url.'">'.esc_attr($title).'</a>';
if(is_feed()) return $link;
else return '<div class="onebox-container"'.$data.'>'.$link.'</div>' ;
}
// register query vars
static function onebox_query_vars_filter($vars) {
$vars[] = "onebox_render";
$vars[] = "onebox_url";
$vars[] = "onebox_title";
$vars[] = "onebox_description";
return $vars;
}
// add template redirect
static function renderOnebox() {
if(get_query_var('onebox_render')) {
include(WP_PLUGIN_DIR.'/onebox/render.php');
exit;
}
}
// add settings link
static function onebox_settings_link($links) {
$settings_link = '<a href="options-general.php?page=onebox">Settings</a>';
array_unshift($links, $settings_link);
return $links;
}
// add tinyMCE button
static function registerTinyMCEButton($buttons){
array_push( $buttons, 'oneboxButton' );
return $buttons;
}
static function registerTinyMCEButtonJS($plugin_array){
$plugin_array['oneboxEditor'] = plugins_url('/js/tinymce-plugin-editor.min.js',__FILE__);
return $plugin_array;
}
// add admin options page
function pluginSettings() {
$page = add_options_page( 'Onebox', 'Onebox', 'manage_options', 'onebox', array ( $this, 'optionsPage' ));
if(get_option('onebox_enable_css')) add_action( 'admin_print_styles-' . $page, array ( $this, 'enqueueStyles' ) );
}
function optionsPage() {
if ( !current_user_can( 'manage_options' ) ) {
wp_die( __( 'You do not have sufficient permissions to access this page.', "onebox" ) );
}
?>
<div class="wrap">
<?php screen_icon(); ?>
<h2><?php _e( 'Onebox Options', 'onebox' ) ?></h2>
<?php
if(!self::isFileGetContentsAllowed()) {
if(!self::isCurlInstalled()) {
?>
<div class="error">
<p><strong><?php _e( 'Error: ', 'onebox'); ?></strong> <?php _e( 'The <a href="http://www.php.net/manual/en/filesystem.configuration.php#ini.allow-url-fopen">allow_url_fopen</a> property (for retrieving data from external websites) is disabled in your PHP configuration, and the <a href="http://www.php.net/manual/en/book.curl.php">cURL extension</a> is not installed.', 'onebox'); ?> <?php _e( 'Onebox requires either of these to function properly.', 'onebox'); ?></p>
</div>
<?php } else { ?>
<div class="updated">
<p><strong><?php _e( 'Notice: ', 'onebox'); ?></strong> <?php _e( 'The <a href="http://www.php.net/manual/en/filesystem.configuration.php#ini.allow-url-fopen">allow_url_fopen</a> property (for retrieving data from external websites) is disabled in your PHP configuration.', 'onebox'); ?> <?php _e( 'Onebox will attempt to use an alternative method (in most cases this should not cause any problems).', 'onebox'); ?></p>
</div>
<?php }
} ?>
<?php if(!self::isGeoIPInstalled()) { ?>
<div class="updated">
<p><strong><?php _e( 'Notice: ', 'onebox'); ?></strong> <?php _e( 'The <a href="http://www.php.net/manual/en/book.geoip.php">GeoIP PHP Extension</a> is not installed.', 'onebox'); ?> <?php _e( 'Some functionality will be disabled.', 'onebox'); ?></p>
</div>
<?php } elseif(!self::isGeoIPWorking()) { ?>
<div class="error">
<p><strong><?php _e( 'Notice: ', 'onebox'); ?></strong> <?php _e( 'The <a href="http://www.php.net/manual/en/book.geoip.php">GeoIP PHP Extension</a> database (GeoIPCity.dat) is not installed.', 'onebox'); ?> <?php _e( 'Some functionality will be disabled.', 'onebox'); ?></p>
</div>
<?php } ?>
<?php if(!self::isAPCCacheInstalled()) { ?>
<div class="updated">
<p><strong><?php _e( 'Notice: ', 'onebox'); ?></strong> <?php _e( 'The <a href="http://php.net/manual/en/book.apc.php">Alternative PHP Cache Extension</a> is not installed.', 'onebox'); ?> <?php _e( 'Caching will be disabled.', 'onebox'); ?></p>
</div>
<?php } ?>
<form action="options.php" method="POST">
<?php settings_fields( 'onebox' ); ?>
<?php do_settings_sections('onebox'); ?>
<?php submit_button(); ?>
</form>
<h2><?php _e( 'Onebox Example', 'onebox' ) ?></h2>
<pre>[onebox url="<?php echo self::$sampleLink; ?>"]</pre>
<?php echo do_shortcode('[onebox url="'.self::$sampleLink.'"]'); ?>
<small><?php _e( 'Actual hyperlink colours and fonts will be based on your theme, and are not represented here', 'onebox'); ?></small>
<p><?php _e( 'For many more live examples, visit the <a href="http://onebox.surrealroad.com">Onebox demo site</a>.', "onebox"); ?></p>
<hr/>
<p><?php _e( 'Onebox Plugin for Wordpress by', 'onebox' ) ?> <a href="http://www.surrealroad.com">Surreal Road</a>. <?php echo self::surrealTagline(); ?>.</p>
<p><?php _e( 'Plugin version', 'onebox' ) ?> <?php echo self::$version; ?></p>
<?php if(get_option('onebox_affiliate_links')) { ?>
<small><?php _e( 'This plugin generates affiliate links in some cases in order to support its development (it does this through Javascript, so the original links are stored/indexed by bots). Modify the source, or do not use it if that makes you uncomfortable.', 'onebox' ) ?></small>
<?php } ?>
</div>
<?php
}
function initTemplateSettings() {
echo '<small>'.__('To experiement with styling options, visit <a href="http://codepen.io/surrealroad/pen/gnzdF">this CodePen</a>.', "safari-push").'</small>';
}
function initFeatureSettings() {
}
function templateHTMLInput(){
self::text_area('onebox_template_html', __( 'HTML template to use for Oneboxes', 'onebox' ) );
}
function templateCSSInput(){
self::checkbox_input('onebox_enable_css', __( 'Enable built-in styles', 'onebox' ) );
}
function templateFlatCSSInput(){
self::checkbox_input('onebox_enable_flat_css', __( 'Enable flat theme', 'onebox' ) );
}
function templateDarkCSSInput(){
self::checkbox_input('onebox_enable_dark_css', __( 'Enable dark theme', 'onebox' ) );
}
function templateSelectorInput(){
self::text_input('onebox_selector', __( 'jQuery selector to use to locate Oneboxes, e.g. ".onebox-container"<br/>Use this if you want to constrain where Oneboxes may be rendered, e.g. ".post .onebox-container"', 'onebox' ) );
}
function apcCacheInput(){
self::checkbox_input('onebox_enable_apc_cache', __( 'Enable APC caching<br/>This is highly recommended for end-user performance.', 'onebox' ), !self::isAPCCacheInstalled() );
}
function githubAPIInput(){
self::text_input('onebox_github_apikey', __( 'GitHub API token (<a href="https://github.com/settings/tokens/new">generate one</a>)<br/>This is required if you plan to use GitHub links on a busy site.', 'onebox' ) );
}
// utility functions
function checkbox_input($option, $description, $disabled=false) {
if (get_option($option)) {
$value = 'checked="checked"';
} else {
$value = '';
}
if($disabled) {
$disabled = 'disabled="disabled"';
} else {
$disabled = '';
}
?>
<input id='<?php echo $option?>' name='<?php echo $option?>' type='checkbox' value='1' <?php echo $value?> <?php echo $disabled?> /> <?php echo $description ?>
<?php
}
function text_input($option, $description) {
if (get_option($option)) {
$value = get_option($option);
} else {
$value = '';
}
?>
<input id='<?php echo $option?>' name='<?php echo $option?>' type='text' value='<?php echo esc_attr( $value ); ?>' />
<br/><?php echo $description ?>
<?php
}
function text_area($option, $description) {
if (get_option($option)) {
$value = get_option($option);
} else {
$value = '';
}
?>
<textarea cols=100 rows=6 id='<?php echo $option?>' name='<?php echo $option?>'><?php echo esc_textarea( $value ); ?></textarea><br><?php echo $description ?>
<?php
}
function surrealTagline() {
$lines = file(plugins_url("/surreal.strings", __FILE__ ) , FILE_IGNORE_NEW_LINES);
return "Hyperlink " . $lines[array_rand($lines)];
}
function isGeoIPInstalled() {
return function_exists("geoip_country_code_by_name");
}
function isGeoIPWorking() {
if(@geoip_record_by_name('php.net')) return true;
return false;
}
function isAPCCacheInstalled() {
return extension_loaded('apc');
}
function isFileGetContentsAllowed() {
return ini_get('allow_url_fopen');
}
function isCurlInstalled() {
if (in_array ('curl', get_loaded_extensions())) {
return true;
}
else{
return false;
}
}
}
// shortcodes (must be declared outside of class)
add_shortcode('onebox', array('OneboxPlugin', 'renderOneboxShortcode'));
$oneboxPlugin = new OneboxPlugin();