/
Axis.php
410 lines (339 loc) · 13.7 KB
/
Axis.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
<?php
/*
Plugin Name: Axis
Version: 1.0.4
Description: Plugin for adding charts to WordPress posts
Author: Ændrew Rininsland
Author URI: http://www.aendrew.com
Plugin URI: http://www.thetimes.co.uk
Text Domain: axiswp
Domain Path: /languages
License: MIT
*/
defined( 'ABSPATH' ) OR exit;
register_activation_hook( __FILE__, array( 'AxisWP', 'on_activation' ) );
register_deactivation_hook( __FILE__, array( 'AxisWP', 'on_deactivation' ) );
register_uninstall_hook( __FILE__, array( 'AxisWP', 'on_uninstall' ) );
add_action( 'plugins_loaded', array( 'AxisWP', 'init' ) );
class AxisWP {
protected static $instance;
/**
* Constructor. Mainly adds filters and actions.
*
* @return null
*/
public function __construct() {
// Backend stuff
if ( is_admin() ) {
add_filter( 'mce_buttons', array( 'AxisWP', 'register_buttons' ) );
add_filter( 'tiny_mce_before_init', array( 'AxisWP', 'tinymce_options' ) );
add_filter( 'mce_external_plugins', array( 'AxisWP', 'register_tinymce_javascript' ) );
add_action( 'admin_enqueue_scripts', array( 'AxisWP', 'add_admin_stylesheet' ) );
add_filter( 'attachment_fields_to_edit', array( 'AxisWP', 'add_chart_metadata_field' ), null, 2 );
add_filter( 'attachment_fields_to_save', array( 'AxisWP', 'save_chart_metadata' ), null, 2 );
add_action( 'wp_ajax_insert_axis_attachment', array( 'AxisWP', 'insert_axis_attachment_callback' ) );
add_action( 'admin_enqueue_scripts', array( 'AxisWP', 'add_frontend_js' ) );
}
// Frontend stuff
add_filter( 'kses_allowed_protocols', array( 'AxisWP', 'allow_data_protocol' ) );
add_filter( 'the_content', array( 'AxisWP', 'convert_png_to_interactive' ) );
add_action( 'wp_enqueue_scripts', array( 'AxisWP', 'add_frontend_js' ) );
add_filter( 'image_send_to_editor', array( 'AxisWP', 'add_data_attribute' ), 10, 7 );
}
// Client-side (frontend) stuff
/**
* Replaces the data-uri PNGs in the backend with a div.
* @TODO Replace DOMDocument with a better DOM parsing library.
*
* @param string $content
* @return string $content
*/
public static function convert_png_to_interactive( $content ) {
$doc = new DOMDocument( '1.0', 'utf-8' );
$phpversion = explode( '.', phpversion() );
$content = mb_convert_encoding( $content, 'HTML-ENTITIES', 'UTF-8' ); // Oh FFS http://stackoverflow.com/a/8218649/467760
if ( $phpversion[1] <= 3 ) {
$doc->loadHTML( $content );
} else {
$doc->loadHTML( $content, LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD ); // Via: http://stackoverflow.com/a/22490902/467760 (Needs PHP >= 5.4)
}
$xpath = new DOMXPath( $doc );
$charts = $xpath->query( "//*[contains(@class, 'axisChart')]" );
foreach ( $charts as $chart ) {
$chartConfig = $chart->getAttribute( 'data-axisjs' );
$div = $doc->createElement( 'div' );
$div->setAttribute( 'data-axisjs', $chartConfig );
$div->setAttribute( 'class', 'axisChart' );
$chart->parentNode->replaceChild( $div, $chart );
}
if ( $phpversion[1] <= 3 ) { // Via: http://stackoverflow.com/a/10657666/467760
// Remove doctype node
$doc->doctype->parentNode->removeChild( $doc->doctype );
// Remove html element, preserving child nodes
$html = $doc->getElementsByTagName( 'html' )->item( 0 );
$fragment = $doc->createDocumentFragment();
while ( $html->childNodes->length > 0 ) {
$fragment->appendChild( $html->childNodes->item( 0 ) );
}
$html->parentNode->replaceChild( $fragment, $html );
// Remove body element, preserving child nodes
$body = $doc->getElementsByTagName( 'body' )->item( 0 );
$fragment = $doc->createDocumentFragment();
while ( $body->childNodes->length > 0 ) {
$fragment->appendChild( $body->childNodes->item( 0 ) );
}
$body->parentNode->replaceChild( $fragment, $body );
}
$content = $doc->saveHTML();
return $content;
}
/**
* Adds frontend JavaScript to the page.
*
* @return null
*/
public static function add_frontend_js() {
wp_enqueue_script( 'd3js', plugins_url( 'bower_components/d3/d3.min.js', __file__ ), array( 'jquery' ), '3.4.11', true );
wp_enqueue_script( 'c3js', plugins_url( 'bower_components/c3/c3.min.js', __file__ ), array( 'jquery', 'd3js' ), '0.3.0', true );
wp_enqueue_style( 'c3jsCSS', plugins_url( 'bower_components/c3/c3.css', __file__ ) );
wp_enqueue_script( 'axis', plugins_url( 'js/axis.js', __file__ ), array( 'jquery', 'c3js', 'd3js' ), '0.1.0', true );
}
// Admin-side backend stuff
/**
* Adding custom field to the $form_fields array
* @see http://code.tutsplus.com/articles/creating-custom-fields-for-attachments-in-wordpress--net-13076
*
* @param array $form_fields
* @param object $post
* @return array
*/
public static function add_chart_metadata_field( $form_fields, $post ) {
// $form_fields is a special array of fields to include in the attachment form
// $post is the attachment record in the database
// $post->post_type == 'attachment'
// (attachments are treated as posts in Wordpress)
$form_fields['axisWP']['input'] = 'hidden';
$form_fields['axisWP']['value'] = get_post_meta( $post->ID, '_axisWP', true );
return $form_fields;
}
/**
* Saving the custom metadata field
*
* @param array $post
* @param array $attachment
* @return array
*/
public static function save_chart_metadata( $post, $attachment ) {
// $attachment part of the form $_POST ($_POST[attachments][postID])
// $post attachments wp post array - will be saved after returned
// $post['post_type'] == 'attachment'
if ( isset( $attachment['_axisWP'] ) && count( $post['tax_input']['chart'] ) > 1 ) {
// update_post_meta(postID, meta_key, meta_value);
update_post_meta( $post['ID'], '_axisWP', $attachment['axisWP'] );
}
return $post;
}
/**
* AJAX callback that inserts chart as attachment into the WP database
*/
public static function insert_axis_attachment_callback() {
// Get config
$axis_config = json_decode( stripslashes( $_POST['axisConfig'] ), true );
// This check might also need a nonce
if ( ! current_user_can( 'upload_files' )
|| ! current_user_can( 'edit_post', $_POST['parentID'] )
|| ( isset( $axis_config['attachmentID'] ) && ! current_user_can( 'edit_post', $axis_config['attachmentID'] ) )
) {
return false;
}
// Begin saving PNG to filesystem
// The following two conditionals are just permissions checks, really.
if ( false === ( $creds = request_filesystem_credentials( 'admin-ajax.php', '', false, false, null ) ) ) {
return false; // stop processing here
}
if ( ! WP_Filesystem( $creds ) ) {
request_filesystem_credentials( 'admin-ajax.php', '', true, false, null );
return false;
}
// Convert data URI to filesystem PNG
global $wp_filesystem;
$upload_dir = wp_upload_dir();
if ( ! isset( $axis_config['attachmentID'] ) ) {
$chartTitle = isset( $axis_config['chartTitle'] ) ? $axis_config['chartTitle'] : 'axisChart';
$chart_filename = sanitize_title_with_dashes( $chartTitle ) . '_' . time() . '.png';
} else {
$chart_filename = basename( wp_get_attachment_url( $axis_config['attachmentID'], true ) );
}
$filename = trailingslashit( $upload_dir['path'] ) . $chart_filename;
$uriPhp = 'data://' . substr( $_POST['axisChart'], 5 ); // Via http://stackoverflow.com/questions/6735414/php-data-uri-to-file/6735458#6735458
if ( function_exists( 'wpcom_vip_file_get_contents' ) ) {
$binary = wpcom_vip_file_get_contents( $uriPhp ); // This might not work like I think it will. Cannot test it...
} else {
$binary = file_get_contents( $uriPhp ); //wpcom_vip_file_get_contents() doesn't exist in normal WP? Bizarre.
}
// Write it to filesystem
$wp_filesystem->put_contents(
$filename,
$binary,
FS_CHMOD_FILE // predefined mode settings for WP files
);
if ( ! isset($axis_config['attachmentID']) ) { // Insert new attachment
$attachment = array(
'guid' => $upload_dir['url'] . '/' . basename( $filename ),
'post_title' => isset( $axis_config['chartTitle'] ) ? $axis_config['chartTitle'] : 'Axis Chart',
'post_content' => '', // Must be empty string
'post_status' => 'published',
'post_mime_type' => 'image/png',
'post_status' => 'inherit',
);
$attach_id = wp_insert_attachment( $attachment, $filename, $_POST['parentID'] );
// Make sure that this file is included, as wp_generate_attachment_metadata() depends on it.
require_once( ABSPATH . 'wp-admin/includes/image.php' );
// Generate the metadata for the attachment, and update the database record.
$attach_data = wp_generate_attachment_metadata( $attach_id, $filename );
wp_update_attachment_metadata( $attach_id, $attach_data );
$axis_config['attachmentID'] = $attach_id;
$attach_url = wp_get_attachment_image_src( $attach_id, 'full' );
$axis_config['attachmentURL'] = $attach_url[0];
update_post_meta( $attach_id, '_axisWP', addslashes( json_encode( $axis_config ) ) ); // Update attachment custom field
echo json_encode( $axis_config ); // Return config to axisJS
die();
} else { // Update existing attachment
$attach_data = wp_generate_attachment_metadata( $axis_config['attachmentID'], $filename );
wp_update_attachment_metadata( $axis_config['attachmentID'], $attach_data );
$attach_url = wp_get_attachment_url( $axis_config['attachmentID'], true ); // Get full path
$axis_config['attachmentURL'] = $attach_url;
update_post_meta( $axis_config['attachmentID'], '_axisWP', json_encode( $axis_config ) );
// Update title (and possibly other metadata) separately
$updates = array(
'ID' => $axis_config['attachmentID'],
'post_title' => (isset($axis_config['chartTitle']) ? $axis_config['chartTitle'] : null),
);
wp_update_post( $updates );
echo json_encode( $axis_config ); // Return config back to axisJS
die();
}
}
/**
* Filter the image HTML markup to send to the editor.
*
* @since 2.5.0
*
* @param string $html The image HTML markup to send.
* @param int $id The attachment id.
* @param string $caption The image caption.
* @param string $title The image title.
* @param string $align The image alignment.
* @param string $url The image source URL.
* @param string $size The image size.
* @param string $alt The image alternative, or alt, text.
*/
public static function add_data_attribute( $html, $id, $alt, $title, $align, $url, $size ) {
$axisJS_config = get_post_meta( $id, '_axisWP', true );
if ( ! empty( $axisJS_config ) ) {
return '<div class="mceNonEditable"><img src="' . $url . '" data-axisjs=\'' . base64_encode( $axisJS_config ) . '\' class="mceItem axisChart" /></div><br />';
} else {
return $html;
}
}
/**
* Adds the AxisWP button to TinyMCE.
*/
public static function register_buttons( $buttons ) {
array_push( $buttons, 'separator', 'Axis' );
return $buttons;
}
/**
* Registers the TinyMCE plugin.
*/
public static function register_tinymce_javascript( $plugin_array ) {
global $wp_version;
$exploded_version = explode( '.', $wp_version );
$major_version = $exploded_version[0];
if ( $major_version > 3 ) {
$plugin_array['Axis'] = plugins_url( '/js/axisJS-tinymce-plugin-wp-4x.js', __file__ );
} else {
$plugin_array['Axis'] = plugins_url( '/js/axisJS-tinymce-plugin.js', __file__ );
}
return $plugin_array;
}
/**
* Enqueues the icon stylesheet.
*/
public static function add_admin_stylesheet() {
wp_enqueue_style( 'axisWP', plugins_url( 'css/axis.css', __file__ ), array( 'dashicons' ), '1.0' );
$params = array(
'axisJSPath' => plugins_url( 'bower_components/axisjs/dist/index.html', __file__ ),
);
global $post;
if ( isset($post->ID) ){
$params['parentID'] = $post->ID;
}
wp_localize_script( 'jquery', 'axisWP', $params ); // Hooking to jQuery just 'cause.
}
/**
* Add to extended_valid_elements for TinyMCE
*
* @param $init assoc. array of TinyMCE options
* @return $init the changed assoc. array
*/
public static function tinymce_options( $init ) {
// Command separated string of extended elements
$ext = '*[*]';
// Add to extended_valid_elements if it alreay exists
if ( isset( $init['extended_valid_elements'] ) ) {
$init['extended_valid_elements'] .= ',' . $ext;
} else {
$init['extended_valid_elements'] = $ext;
}
$init['paste_data_images'] = true;
// Super important: return $init!
return $init;
}
/**
* Enables saving the charts as data-URI PNGs.
*/
public static function allow_data_protocol( $protocols ){
$protocols[] = 'data';
return $protocols;
}
// Installation/Deactivation/Uninstallation
// Based on: http://wordpress.stackexchange.com/a/25979/1682
public static function init() {
is_null( self::$instance ) AND self::$instance = new self;
return self::$instance;
}
public static function on_activation() {
if ( ! current_user_can( 'activate_plugins' ) ) {
return;
}
$plugin_req = $_REQUEST['plugin'];
$plugin = isset( $plugin_req ) ? $plugin_req : '';
check_admin_referer( "activate-plugin_{$plugin}" );
# Uncomment the following line to see the function in action
# exit( var_dump( $_GET ) );
}
public static function on_deactivation() {
if ( ! current_user_can( 'activate_plugins' ) ) {
return;
}
$plugin_req = $_REQUEST['plugin'];
$plugin = isset( $plugin_req ) ? $plugin_req : '';
check_admin_referer( "deactivate-plugin_{$plugin}" );
# Uncomment the following line to see the function in action
# exit( var_dump( $_GET ) );
}
public static function on_uninstall() {
if ( ! current_user_can( 'activate_plugins' ) ) {
return;
}
check_admin_referer( 'bulk-plugins' );
// Important: Check if the file is the one
// that was registered during the uninstall hook.
if ( __FILE__ != WP_UNINSTALL_PLUGIN ) {
return;
}
# Uncomment the following line to see the function in action
# exit( var_dump( $_GET ) );
}
}