PluginProbe
TablePress – Tables in WordPress made easy / 2.1.7
TablePress – Tables in WordPress made easy v2.1.7
3.3.4 3.3.3 3.3.2 3.3.1 trunk 1.12 1.14 1.9.2 2.0.4 2.1.7 2.1.8 2.2 2.2.1 2.2.2 2.2.3 2.2.4 2.2.5 2.3 2.3.1 2.3.2 2.4 2.4.1 2.4.2 2.4.3 2.4.4 All 44 releases
tablepress / classes / class-admin-page-helper.php

class-admin-page-helper.php in TablePress – Tables in WordPress made easy 2.1.7, at classes/class-admin-page-helper.php

181 lines 6.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Admin Page Helper Class for TablePress with functions needed in the admin area
4 *
5 * @package TablePress
6 * @subpackage Views
7 * @author Tobias Bäthge
8 * @since 1.0.0
9 */
10
11 // Prohibit direct script loading.
12 defined( 'ABSPATH' ) || die( 'No direct script access allowed!' );
13
14 /**
15 * Admin Page class
16 *
17 * @package TablePress
18 * @subpackage Views
19 * @author Tobias Bäthge
20 * @since 1.0.0
21 */
22 class TablePress_Admin_Page {
23
24 /**
25 * Enqueue a CSS file, possibly with dependencies.
26 *
27 * @since 1.0.0
28 *
29 * @param string $name Name of the CSS file, without extension.
30 * @param array $dependencies Optional. List of names of CSS stylesheets that this stylesheet depends on, and which need to be included before this one.
31 */
32 public function enqueue_style( $name, array $dependencies = array() ) {
33 $css_file = "admin/css/build/{$name}.css";
34 $css_url = plugins_url( $css_file, TABLEPRESS__FILE__ );
35 wp_enqueue_style( "tablepress-{$name}", $css_url, $dependencies, TablePress::version );
36 }
37
38 /**
39 * Enqueue a JavaScript file, possibly with dependencies and extra information.
40 *
41 * @since 1.0.0
42 *
43 * @param string $name Name of the JS file, without extension.
44 * @param array $dependencies Optional. List of names of JS scripts that this script depends on, and which need to be included before this one.
45 * @param array $script_data Optional. JS data that is printed to the page before the script is included. The array key will be used as the name, the value will be JSON encoded.
46 */
47 public function enqueue_script( $name, array $dependencies = array(), array $script_data = array() ) {
48 $js_file = "admin/js/build/{$name}.js";
49 $js_url = plugins_url( $js_file, TABLEPRESS__FILE__ );
50
51 $version = TablePress::version;
52
53 // Load dependencies and version from the auto-generated asset PHP file.
54 $script_asset_path = TABLEPRESS_ABSPATH . "admin/js/build/{$name}.asset.php";
55 if ( file_exists( $script_asset_path ) ) {
56 $script_asset = require $script_asset_path;
57 if ( isset( $script_asset['dependencies'] ) ) {
58 $dependencies = array_merge( $dependencies, $script_asset['dependencies'] );
59 }
60 if ( isset( $script_asset['version'] ) ) {
61 $version = $script_asset['version'];
62 }
63 }
64
65 /**
66 * Filters the dependencies of a TablePress script file.
67 *
68 * @since 2.0.0
69 *
70 * @param array $dependencies List of the dependencies that the $name script relies on.
71 * @param string $name Name of the JS script, without extension.
72 */
73 $dependencies = apply_filters( 'tablepress_admin_page_script_dependencies', $dependencies, $name );
74
75 wp_enqueue_script( "tablepress-{$name}", $js_url, $dependencies, $version, true );
76
77 // Load JavaScript translation files, for all scripts that rely on `wp-i18n`.
78 if ( in_array( 'wp-i18n', $dependencies, true ) ) {
79 wp_set_script_translations( "tablepress-{$name}", 'tablepress' );
80 }
81
82 if ( ! empty( $script_data ) ) {
83 foreach ( $script_data as $var_name => $var_data ) {
84 $var_data = wp_json_encode( $var_data, JSON_FORCE_OBJECT );
85 wp_add_inline_script( "tablepress-{$name}", "const tablepress_{$var_name} = {$var_data};", 'before' );
86 }
87 }
88 }
89
90 /**
91 * Register a filter hook on the admin footer.
92 *
93 * @since 1.0.0
94 */
95 public function add_admin_footer_text() {
96 // Show admin footer message (only on TablePress admin screens).
97 add_filter( 'admin_footer_text', array( $this, '_admin_footer_text' ) );
98 }
99
100 /**
101 * Add a TablePress "Thank You" message to the admin footer content.
102 *
103 * @since 1.0.0
104 *
105 * @param string $content Current admin footer content.
106 * @return string New admin footer content.
107 */
108 public function _admin_footer_text( $content ) {
109 $content .= ' &bull; ' . sprintf( __( 'Thank you for using <a href="%s">TablePress</a>.', 'tablepress' ), 'https://tablepress.org/' );
110 if ( tb_tp_fs()->is_free_plan() ) {
111 $content .= ' ' . sprintf( __( 'Take a look at the <a href="%s">Premium features</a>!', 'tablepress' ), 'https://tablepress.org/premium/' );
112 }
113 return $content;
114 }
115
116 /**
117 * Print the JavaScript code for a WP feature pointer.
118 *
119 * @since 1.0.0
120 *
121 * @param string $pointer_id The pointer ID.
122 * @param string $selector The HTML elements, on which the pointer should be attached.
123 * @param array $args Arguments to be passed to the pointer JS (see wp-pointer.js).
124 */
125 public function print_wp_pointer_js( $pointer_id, $selector, array $args ) {
126 if ( empty( $pointer_id ) || empty( $selector ) || empty( $args['content'] ) ) {
127 return;
128 }
129
130 /*
131 * Print JS code for the feature pointers, extened with event handling for opened/closed "Screen Options", so that pointers can
132 * be repositioned. 210 ms is slightly slower than jQuery's "fast" value, to allow all elements to reach their original position.
133 */
134 ?>
135 <script>
136 ( function( $ ) {
137 let options = <?php echo wp_json_encode( $args, TABLEPRESS_JSON_OPTIONS ); ?>;
138
139 if ( ! options ) {
140 return;
141 }
142
143 options = $.extend( options, {
144 close: function() {
145 $.post( ajaxurl, {
146 pointer: '<?php echo $pointer_id; ?>',
147 action: 'dismiss-wp-pointer'
148 } );
149 $( this ).pointer( { 'disabled': true } );
150 }
151 } );
152
153 $( function () {
154 $( '<?php echo $selector; ?>' ).pointer( options ).pointer( 'open' );
155 } );
156
157 $( document ).on( 'screen:options:open screen:options:close', function () {
158 setTimeout( function () { $( '<?php echo $selector; ?>' ).pointer( 'reposition' ); }, 210 );
159 } );
160 } )( jQuery );
161 </script>
162 <?php
163 }
164
165 /**
166 * Returns a safe JSON representation of a variable for printing inside of JavaScript code.
167 *
168 * @since 2.0.0
169 *
170 * @param mixed $data Variable to convert to JSON.
171 * @return string Safe JSON representation of a variable for printing inside of JavaScript code.
172 */
173 public function convert_to_json_parse_output( $data ) {
174 $json = wp_json_encode( $data, TABLEPRESS_JSON_OPTIONS );
175 // Print them inside a `JSON.parse()` call in JS for speed gains, with necessary escaping of `</script>`, `'`, and `\`.
176 $json = str_replace( array( '</script>', '\\', "'" ), array( '<\/script>', '\\\\', "\'" ), $json );
177 return "JSON.parse( '{$json}' )";
178 }
179
180 } // class TablePress_Admin_Page
181