| 1 |
<?php |
| 2 |
|
| 3 |
// +----------------------------------------------------------------------+ |
| 4 |
// | Copyright 2013 Madpixels (email : visualizer@madpixels.net) | |
| 5 |
// +----------------------------------------------------------------------+ |
| 6 |
// | This program is free software; you can redistribute it and/or modify | |
| 7 |
// | it under the terms of the GNU General Public License, version 2, as | |
| 8 |
// | published by the Free Software Foundation. | |
| 9 |
// | | |
| 10 |
// | This program is distributed in the hope that it will be useful, | |
| 11 |
// | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
| 12 |
// | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
| 13 |
// | GNU General Public License for more details. | |
| 14 |
// | | |
| 15 |
// | You should have received a copy of the GNU General Public License | |
| 16 |
// | along with this program; if not, write to the Free Software | |
| 17 |
// | Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, | |
| 18 |
// | MA 02110-1301 USA | |
| 19 |
// +----------------------------------------------------------------------+ |
| 20 |
// | Author: Eugene Manuilov <eugene@manuilov.org> | |
| 21 |
// +----------------------------------------------------------------------+ |
| 22 |
|
| 23 |
/** |
| 24 |
* The module for all admin stuff. |
| 25 |
* |
| 26 |
* @category Visualizer |
| 27 |
* @package Module |
| 28 |
* |
| 29 |
* @since 1.0.0 |
| 30 |
*/ |
| 31 |
class Visualizer_Module_Admin extends Visualizer_Module { |
| 32 |
|
| 33 |
const NAME = __CLASS__; |
| 34 |
|
| 35 |
/** |
| 36 |
* Library page suffix. |
| 37 |
* |
| 38 |
* @since 1.0.0 |
| 39 |
* |
| 40 |
* @access private |
| 41 |
* @var string |
| 42 |
*/ |
| 43 |
private $_libraryPage; |
| 44 |
|
| 45 |
/** |
| 46 |
* Constructor. |
| 47 |
* |
| 48 |
* @since 1.0.0 |
| 49 |
* |
| 50 |
* @access public |
| 51 |
* @param Visualizer_Plugin $plugin The instance of the plugin. |
| 52 |
*/ |
| 53 |
public function __construct( Visualizer_Plugin $plugin ) { |
| 54 |
parent::__construct( $plugin ); |
| 55 |
|
| 56 |
$this->_addAction( 'load-post.php', 'enqueueMediaScripts' ); |
| 57 |
$this->_addAction( 'load-post-new.php', 'enqueueMediaScripts' ); |
| 58 |
$this->_addAction( 'admin_footer', 'renderTempaltes' ); |
| 59 |
$this->_addAction( 'admin_enqueue_scripts', 'enqueueLibraryScripts' ); |
| 60 |
$this->_addAction( 'admin_menu', 'registerAdminMenu' ); |
| 61 |
|
| 62 |
$this->_addFilter( 'media_view_strings', 'setupMediaViewStrings' ); |
| 63 |
$this->_addFilter( 'plugin_action_links', 'getPluginActionLinks', 10, 2 ); |
| 64 |
$this->_addFilter( 'plugin_row_meta', 'getPluginMetaLinks', 10, 2 ); |
| 65 |
} |
| 66 |
|
| 67 |
/** |
| 68 |
* Returns associated array of chart types and localized names. |
| 69 |
* |
| 70 |
* @since 1.0.0 |
| 71 |
* |
| 72 |
* @static |
| 73 |
* @access private |
| 74 |
* @return array The associated array of chart types with localized names. |
| 75 |
*/ |
| 76 |
private static function _getChartTypesLocalized() { |
| 77 |
return array( |
| 78 |
'all' => esc_html__( 'All', Visualizer_Plugin::NAME ), |
| 79 |
'pie' => esc_html__( 'Pie', Visualizer_Plugin::NAME ), |
| 80 |
'line' => esc_html__( 'Line', Visualizer_Plugin::NAME ), |
| 81 |
'area' => esc_html__( 'Area', Visualizer_Plugin::NAME ), |
| 82 |
'geo' => esc_html__( 'Geo', Visualizer_Plugin::NAME ), |
| 83 |
'bar' => esc_html__( 'Bar', Visualizer_Plugin::NAME ), |
| 84 |
'column' => esc_html__( 'Column', Visualizer_Plugin::NAME ), |
| 85 |
'gauge' => esc_html__( 'Gauge', Visualizer_Plugin::NAME ), |
| 86 |
'scatter' => esc_html__( 'Scatter', Visualizer_Plugin::NAME ), |
| 87 |
'candlestick' => esc_html__( 'Candelstick', Visualizer_Plugin::NAME ), |
| 88 |
); |
| 89 |
} |
| 90 |
|
| 91 |
/** |
| 92 |
* Enqueues media scripts and styles. |
| 93 |
* |
| 94 |
* @since 1.0.0 |
| 95 |
* @uses wp_enqueue_style To enqueue style file. |
| 96 |
* @uses wp_enqueue_script To enqueue script file. |
| 97 |
* |
| 98 |
* @access public |
| 99 |
*/ |
| 100 |
public function enqueueMediaScripts() { |
| 101 |
global $typenow; |
| 102 |
|
| 103 |
if ( post_type_supports( $typenow, 'editor' ) ) { |
| 104 |
wp_enqueue_style( 'visualizer-media', VISUALIZER_ABSURL . 'css/media.css', array( 'media-views' ), Visualizer_Plugin::VERSION ); |
| 105 |
|
| 106 |
wp_enqueue_script( 'visualizer-google-jsapi', '//www.google.com/jsapi', array( 'media-editor' ), null, true ); |
| 107 |
wp_enqueue_script( 'visualizer-media-model', VISUALIZER_ABSURL . 'js/media/model.js', array( 'visualizer-google-jsapi' ), Visualizer_Plugin::VERSION, true ); |
| 108 |
wp_enqueue_script( 'visualizer-media-collection', VISUALIZER_ABSURL . 'js/media/collection.js', array( 'visualizer-media-model' ), Visualizer_Plugin::VERSION, true ); |
| 109 |
wp_enqueue_script( 'visualizer-media-controller', VISUALIZER_ABSURL . 'js/media/controller.js', array( 'visualizer-media-collection' ), Visualizer_Plugin::VERSION, true ); |
| 110 |
wp_enqueue_script( 'visualizer-media-view', VISUALIZER_ABSURL . 'js/media/view.js', array( 'visualizer-media-controller' ), Visualizer_Plugin::VERSION, true ); |
| 111 |
wp_enqueue_script( 'visualizer-media-toolbar', VISUALIZER_ABSURL . 'js/media/toolbar.js', array( 'visualizer-media-view' ), Visualizer_Plugin::VERSION, true ); |
| 112 |
wp_enqueue_script( 'visualizer-media', VISUALIZER_ABSURL . 'js/media.js', array( 'visualizer-media-toolbar' ), Visualizer_Plugin::VERSION, true ); |
| 113 |
} |
| 114 |
} |
| 115 |
|
| 116 |
/** |
| 117 |
* Extends media view strings with visualizer strings. |
| 118 |
* |
| 119 |
* @since 1.0.0 |
| 120 |
* |
| 121 |
* @access public |
| 122 |
* @param array $strings The array of media view strings. |
| 123 |
* @return array The extended array of media view strings. |
| 124 |
*/ |
| 125 |
public function setupMediaViewStrings( $strings ) { |
| 126 |
$strings['visualizer'] = array( |
| 127 |
'actions' => array( |
| 128 |
'get_charts' => Visualizer_Plugin::ACTION_GET_CHARTS, |
| 129 |
'delete_chart' => Visualizer_Plugin::ACTION_DELETE_CHART, |
| 130 |
), |
| 131 |
'controller' => array( |
| 132 |
'title' => esc_html__( 'Visualizations', Visualizer_Plugin::NAME ), |
| 133 |
), |
| 134 |
'routers' => array( |
| 135 |
'library' => esc_html__( 'From Library', Visualizer_Plugin::NAME ), |
| 136 |
'create' => esc_html__( 'Create New', Visualizer_Plugin::NAME ), |
| 137 |
), |
| 138 |
'library' => array( |
| 139 |
'filters' => self::_getChartTypesLocalized(), |
| 140 |
), |
| 141 |
'nonce' => wp_create_nonce(), |
| 142 |
'buildurl' => add_query_arg( 'action', Visualizer_Plugin::ACTION_CREATE_CHART, admin_url( 'admin-ajax.php' ) ), |
| 143 |
); |
| 144 |
|
| 145 |
return $strings; |
| 146 |
} |
| 147 |
|
| 148 |
/** |
| 149 |
* Renders templates to use in media popup. |
| 150 |
* |
| 151 |
* @since 1.0.0 |
| 152 |
* @global string $pagenow The name of the current page. |
| 153 |
* |
| 154 |
* @access public |
| 155 |
*/ |
| 156 |
public function renderTempaltes() { |
| 157 |
global $pagenow; |
| 158 |
|
| 159 |
if ( 'post.php' != $pagenow && 'post-new.php' != $pagenow ) { |
| 160 |
return; |
| 161 |
} |
| 162 |
|
| 163 |
$render = new Visualizer_Render_Templates(); |
| 164 |
$render->render(); |
| 165 |
} |
| 166 |
|
| 167 |
/** |
| 168 |
* Enqueues library scripts and styles. |
| 169 |
* |
| 170 |
* @since 1.0.0 |
| 171 |
* @uses wp_enqueue_style() To enqueue library stylesheet. |
| 172 |
* @uses wp_enqueue_script() To enqueue javascript file. |
| 173 |
* @uses wp_enqueue_media() To enqueue media stuff. |
| 174 |
* |
| 175 |
* @access public |
| 176 |
* @param string $suffix The current page suffix. |
| 177 |
*/ |
| 178 |
public function enqueueLibraryScripts( $suffix ) { |
| 179 |
if ( $suffix == $this->_libraryPage ) { |
| 180 |
wp_enqueue_style( 'visualizer-library', VISUALIZER_ABSURL . 'css/library.css', array(), Visualizer_Plugin::VERSION ); |
| 181 |
|
| 182 |
$this->_addFilter( 'media_upload_tabs', 'setupVisualizerTab' ); |
| 183 |
|
| 184 |
wp_enqueue_media(); |
| 185 |
wp_enqueue_script( 'visualizer-library', VISUALIZER_ABSURL . 'js/library.js', array( 'jquery', 'media-views' ), Visualizer_Plugin::VERSION, true ); |
| 186 |
wp_enqueue_script( 'google-jsapi', '//www.google.com/jsapi', array(), null, true ); |
| 187 |
wp_enqueue_script( 'visualizer-render', VISUALIZER_ABSURL . 'js/render.js', array( 'google-jsapi', 'visualizer-library' ), Visualizer_Plugin::VERSION, true ); |
| 188 |
} |
| 189 |
} |
| 190 |
|
| 191 |
/** |
| 192 |
* Adds visualizer tab for media upload tabs array. |
| 193 |
* |
| 194 |
* @since 1.0.0 |
| 195 |
* |
| 196 |
* @access public |
| 197 |
* @param array $tabs The array of media upload tabs. |
| 198 |
* @return array Extended array of media upload tabs. |
| 199 |
*/ |
| 200 |
public function setupVisualizerTab( $tabs ) { |
| 201 |
$tabs['visualizer'] = 'Visualizer'; |
| 202 |
return $tabs; |
| 203 |
} |
| 204 |
|
| 205 |
/** |
| 206 |
* Registers admin menu for visualizer library. |
| 207 |
* |
| 208 |
* @since 1.0.0 |
| 209 |
* |
| 210 |
* @access public |
| 211 |
*/ |
| 212 |
public function registerAdminMenu() { |
| 213 |
$title = esc_html__( 'Visualizer Library', Visualizer_Plugin::NAME ); |
| 214 |
$callback = array( $this, 'renderLibraryPage' ); |
| 215 |
$this->_libraryPage = add_submenu_page( 'upload.php', $title, $title, 'edit_posts', Visualizer_Plugin::NAME, $callback ); |
| 216 |
} |
| 217 |
|
| 218 |
/** |
| 219 |
* Renders visualizer library page. |
| 220 |
* |
| 221 |
* @since 1.0.0 |
| 222 |
* |
| 223 |
* @access public |
| 224 |
*/ |
| 225 |
public function renderLibraryPage() { |
| 226 |
// get current page |
| 227 |
$page = filter_input( INPUT_GET, 'vpage', FILTER_VALIDATE_INT, array( |
| 228 |
'options' => array( |
| 229 |
'min_range' => 1, |
| 230 |
'default' => 1, |
| 231 |
) |
| 232 |
) ); |
| 233 |
|
| 234 |
// the initial query arguments to fetch charts |
| 235 |
$query_args = array( |
| 236 |
'post_type' => Visualizer_Plugin::CPT_VISUALIZER, |
| 237 |
'posts_per_page' => 6, |
| 238 |
'paged' => $page, |
| 239 |
); |
| 240 |
|
| 241 |
// add chart type filter to the query arguments |
| 242 |
$filter = filter_input( INPUT_GET, 'type' ); |
| 243 |
if ( $filter && in_array( $filter, Visualizer_Plugin::getChartTypes() ) ) { |
| 244 |
$query_args['meta_query'] = array( |
| 245 |
array( |
| 246 |
'key' => Visualizer_Plugin::CF_CHART_TYPE, |
| 247 |
'value' => $filter, |
| 248 |
'compare' => '=', |
| 249 |
), |
| 250 |
); |
| 251 |
} else { |
| 252 |
$filter = 'all'; |
| 253 |
} |
| 254 |
|
| 255 |
// fetch charts |
| 256 |
$charts = array(); |
| 257 |
$query = new WP_Query( $query_args ); |
| 258 |
while( $query->have_posts() ) { |
| 259 |
$chart = $query->next_post(); |
| 260 |
|
| 261 |
// fetch and update settings |
| 262 |
$settings = get_post_meta( $chart->ID, Visualizer_Plugin::CF_SETTINGS, true ); |
| 263 |
unset( $settings['height'], $settings['width'] ); |
| 264 |
|
| 265 |
$type = get_post_meta( $chart->ID, Visualizer_Plugin::CF_CHART_TYPE, true ); |
| 266 |
$series = apply_filters( Visualizer_Plugin::FILTER_GET_CHART_SERIES, get_post_meta( $chart->ID, Visualizer_Plugin::CF_SERIES, true ), $chart->ID, $type ); |
| 267 |
$data = apply_filters( Visualizer_Plugin::FILTER_GET_CHART_DATA, unserialize( $chart->post_content ), $chart->ID, $type ); |
| 268 |
|
| 269 |
// add chart to the array |
| 270 |
$charts['visualizer-' . $chart->ID] = array( |
| 271 |
'id' => $chart->ID, |
| 272 |
'type' => $type, |
| 273 |
'series' => $series, |
| 274 |
'settings' => $settings, |
| 275 |
'data' => $data, |
| 276 |
); |
| 277 |
} |
| 278 |
|
| 279 |
// enqueue charts array |
| 280 |
$ajaxurl = admin_url( 'admin-ajax.php' ); |
| 281 |
wp_localize_script( 'visualizer-library', 'visualizer', array( |
| 282 |
'charts' => $charts, |
| 283 |
'urls' => array( |
| 284 |
'base' => add_query_arg( 'vpage', false ), |
| 285 |
'create' => add_query_arg( array( 'action' => Visualizer_Plugin::ACTION_CREATE_CHART, 'library' => 'yes' ), $ajaxurl ), |
| 286 |
'edit' => add_query_arg( array( 'action' => Visualizer_Plugin::ACTION_EDIT_CHART, 'library' => 'yes' ), $ajaxurl ), |
| 287 |
), |
| 288 |
) ); |
| 289 |
|
| 290 |
// render library page |
| 291 |
$render = new Visualizer_Render_Library(); |
| 292 |
|
| 293 |
$render->charts = $charts; |
| 294 |
$render->type = $filter; |
| 295 |
$render->types = self::_getChartTypesLocalized(); |
| 296 |
$render->pagination = paginate_links( array( |
| 297 |
'base' => add_query_arg( 'vpage', '%#%' ), |
| 298 |
'format' => '', |
| 299 |
'current' => $page, |
| 300 |
'total' => $query->max_num_pages, |
| 301 |
'type' => 'array', |
| 302 |
) ); |
| 303 |
|
| 304 |
$render->render(); |
| 305 |
} |
| 306 |
|
| 307 |
/** |
| 308 |
* Updates the plugin's action links, which will be rendered at the plugins table. |
| 309 |
* |
| 310 |
* @since 1.0.0 |
| 311 |
* |
| 312 |
* @access public |
| 313 |
* @param array $links The array of original action links. |
| 314 |
* @param string $file The plugin basename. |
| 315 |
* @return array Updated array of action links. |
| 316 |
*/ |
| 317 |
public function getPluginActionLinks( $links, $file ) { |
| 318 |
if ( $file == plugin_basename( VISUALIZER_BASEFILE ) ) { |
| 319 |
array_unshift( |
| 320 |
$links, |
| 321 |
sprintf( |
| 322 |
'<a href="%s">%s</a>', |
| 323 |
admin_url( 'upload.php?page=' . Visualizer_Plugin::NAME ), |
| 324 |
esc_html__( 'Library', Visualizer_Plugin::NAME ) |
| 325 |
) |
| 326 |
); |
| 327 |
} |
| 328 |
|
| 329 |
return $links; |
| 330 |
} |
| 331 |
|
| 332 |
/** |
| 333 |
* Updates the plugin's meta links, which will be rendered at the plugins table. |
| 334 |
* |
| 335 |
* @since 1.0.0 |
| 336 |
* |
| 337 |
* @access public |
| 338 |
* @param array $plugin_meta The array of a plugin meta links. |
| 339 |
* @param string $plugin_file The plugin's basename. |
| 340 |
* @return array Updated array of plugin meta links. |
| 341 |
*/ |
| 342 |
public function getPluginMetaLinks( $plugin_meta, $plugin_file ) { |
| 343 |
if ( $plugin_file == plugin_basename( VISUALIZER_BASEFILE ) ) { |
| 344 |
// knowledge base link |
| 345 |
$plugin_meta[] = sprintf( |
| 346 |
'<a href="https://github.com/madpixelslabs/visualizer/wiki" target="_blank">%s</a>', |
| 347 |
esc_html__( 'Knowledge Base', Visualizer_Plugin::NAME ) |
| 348 |
); |
| 349 |
// flattr link |
| 350 |
$plugin_meta[] = sprintf( |
| 351 |
'<a href="http://flattr.com/thing/2574985/WordPress-Visualizer" target="_blank">%s</a>', |
| 352 |
esc_html__( 'Donate', Visualizer_Plugin::NAME ) |
| 353 |
); |
| 354 |
} |
| 355 |
|
| 356 |
return $plugin_meta; |
| 357 |
} |
| 358 |
|
| 359 |
} |