| 1 |
<?php |
| 2 |
// +----------------------------------------------------------------------+ |
| 3 |
// | Copyright 2013 Madpixels (email : visualizer@madpixels.net) | |
| 4 |
// +----------------------------------------------------------------------+ |
| 5 |
// | This program is free software; you can redistribute it and/or modify | |
| 6 |
// | it under the terms of the GNU General Public License, version 2, as | |
| 7 |
// | published by the Free Software Foundation. | |
| 8 |
// | | |
| 9 |
// | This program is distributed in the hope that it will be useful, | |
| 10 |
// | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
| 11 |
// | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
| 12 |
// | GNU General Public License for more details. | |
| 13 |
// | | |
| 14 |
// | You should have received a copy of the GNU General Public License | |
| 15 |
// | along with this program; if not, write to the Free Software | |
| 16 |
// | Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, | |
| 17 |
// | MA 02110-1301 USA | |
| 18 |
// +----------------------------------------------------------------------+ |
| 19 |
// | Author: Eugene Manuilov <eugene@manuilov.org> | |
| 20 |
// +----------------------------------------------------------------------+ |
| 21 |
/** |
| 22 |
* The module for all AMP stuff. |
| 23 |
* |
| 24 |
* @category Visualizer |
| 25 |
* @package Module |
| 26 |
* |
| 27 |
* @since 1.0.0 |
| 28 |
*/ |
| 29 |
class Visualizer_Module_AMP extends Visualizer_Module { |
| 30 |
|
| 31 |
const NAME = __CLASS__; |
| 32 |
|
| 33 |
/** |
| 34 |
* Constructor. |
| 35 |
* |
| 36 |
* @since 1.0.0 |
| 37 |
* |
| 38 |
* @access public |
| 39 |
* |
| 40 |
* @param Visualizer_Plugin $plugin The instance of the plugin. |
| 41 |
*/ |
| 42 |
public function __construct( Visualizer_Plugin $plugin ) { |
| 43 |
$this->_addFilter( 'amp_post_template_data', 'addToHeader' ); |
| 44 |
} |
| 45 |
|
| 46 |
/** |
| 47 |
* Add the iframe component to the header. |
| 48 |
*/ |
| 49 |
public function addToHeader( $data ) { |
| 50 |
$data['amp_component_scripts'] = array_merge( |
| 51 |
$data['amp_component_scripts'], |
| 52 |
array( |
| 53 |
'amp-iframe' => 'https://cdn.ampproject.org/v0/amp-iframe-latest.js', |
| 54 |
) |
| 55 |
); |
| 56 |
return $data; |
| 57 |
} |
| 58 |
|
| 59 |
/** |
| 60 |
* Is this an AMP request? |
| 61 |
*/ |
| 62 |
public static function is_amp() { |
| 63 |
return function_exists( 'is_amp_endpoint' ) && is_amp_endpoint(); |
| 64 |
} |
| 65 |
|
| 66 |
/** |
| 67 |
* Loads the alternate view of the chart. |
| 68 |
*/ |
| 69 |
public function get_chart( $chart, $data, $series, $settings ) { |
| 70 |
// let's check if there is a view parameter in the GET request. |
| 71 |
$view = isset( $_GET['view'] ) ? $_GET['view'] : null; |
| 72 |
|
| 73 |
if ( ! empty( $view ) ) { |
| 74 |
// attachmend id? |
| 75 |
if ( is_numeric( $view ) ) { |
| 76 |
$view = wp_get_attachment_url( $view ); |
| 77 |
} |
| 78 |
|
| 79 |
$attributes = wp_check_filetype( $view ); |
| 80 |
if ( $attributes && false !== strpos( $attributes['type'], 'image/' ) ) { |
| 81 |
// absolute url or relative? |
| 82 |
if ( 0 !== strpos( $view, 'http' ) ) { |
| 83 |
$view = site_url( $view ); |
| 84 |
} |
| 85 |
$view = '<img src="' . $view . '">'; |
| 86 |
} |
| 87 |
|
| 88 |
if ( ! empty( $view ) ) { |
| 89 |
return $view; |
| 90 |
} |
| 91 |
} |
| 92 |
|
| 93 |
$view = apply_filters( 'visualizer_amp_view', null, $chart, $data, $series, $settings ); |
| 94 |
if ( ! is_null( $view ) ) { |
| 95 |
return $view; |
| 96 |
} |
| 97 |
$output = $this->_getDataAs( $chart->ID, 'image' ); |
| 98 |
if ( empty( $output['csv'] ) ) { |
| 99 |
$output = $this->_getDataAs( $chart->ID, 'print' ); |
| 100 |
} |
| 101 |
return $output['csv']; |
| 102 |
} |
| 103 |
} |
| 104 |
|