| 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 |
* Sources module class. |
| 23 |
* |
| 24 |
* @category Visualizer |
| 25 |
* @package Module |
| 26 |
* |
| 27 |
* @since 1.1.0 |
| 28 |
*/ |
| 29 |
class Visualizer_Module_Sources extends Visualizer_Module { |
| 30 |
|
| 31 |
const NAME = __CLASS__; |
| 32 |
|
| 33 |
/** |
| 34 |
* The array of fetched sources. |
| 35 |
* |
| 36 |
* @since 1.1.0 |
| 37 |
* |
| 38 |
* @access private |
| 39 |
* @var array |
| 40 |
*/ |
| 41 |
private $_sources = array(); |
| 42 |
|
| 43 |
/** |
| 44 |
* Constructor. |
| 45 |
* |
| 46 |
* @since 1.1.0 |
| 47 |
* |
| 48 |
* @access public |
| 49 |
* |
| 50 |
* @param Visualizer_Plugin $plugin The instance of the plugin. |
| 51 |
*/ |
| 52 |
public function __construct( Visualizer_Plugin $plugin ) { |
| 53 |
parent::__construct( $plugin ); |
| 54 |
$this->_addFilter( Visualizer_Plugin::FILTER_GET_CHART_SERIES, 'filterChartSeries', 1, 2 ); |
| 55 |
$this->_addFilter( Visualizer_Plugin::FILTER_GET_CHART_DATA, 'filterChartData', 1, 2 ); |
| 56 |
$this->_addFilter( 'visualizer_pro_upsell', 'addProUpsell', 10, 3 ); |
| 57 |
} |
| 58 |
|
| 59 |
/** |
| 60 |
* Filters chart sereis. |
| 61 |
* |
| 62 |
* @since 1.1.0 |
| 63 |
* |
| 64 |
* @access public |
| 65 |
* |
| 66 |
* @param array $series The array of chart series. |
| 67 |
* @param int $chart_id The chart id. |
| 68 |
* |
| 69 |
* @return array The array of filtered series. |
| 70 |
*/ |
| 71 |
public function filterChartSeries( $series, $chart_id ) { |
| 72 |
$source = $this->_getSource( $chart_id ); |
| 73 |
if ( ! $source ) { |
| 74 |
return $series; |
| 75 |
} |
| 76 |
|
| 77 |
return $source->repopulateSeries( $series, $chart_id ); |
| 78 |
} |
| 79 |
|
| 80 |
/** |
| 81 |
* Returns appropriate source object for a chart. |
| 82 |
* |
| 83 |
* @since 1.1.0 |
| 84 |
* |
| 85 |
* @access private |
| 86 |
* |
| 87 |
* @param int $chart_id The chart id. |
| 88 |
* |
| 89 |
* @return Visualizer_Source The source object if source exists, otherwise FALSE. |
| 90 |
*/ |
| 91 |
private function _getSource( $chart_id ) { |
| 92 |
if ( ! isset( $this->_sources[ $chart_id ] ) ) { |
| 93 |
$class = get_post_meta( $chart_id, Visualizer_Plugin::CF_SOURCE, true ); |
| 94 |
if ( ! class_exists( $class, true ) ) { |
| 95 |
return false; |
| 96 |
} |
| 97 |
$this->_sources[ $chart_id ] = new $class(); |
| 98 |
} |
| 99 |
|
| 100 |
return $this->_sources[ $chart_id ]; |
| 101 |
} |
| 102 |
|
| 103 |
/** |
| 104 |
* Filters chart data. |
| 105 |
* |
| 106 |
* @since 1.1.0 |
| 107 |
* |
| 108 |
* @access public |
| 109 |
* |
| 110 |
* @param array $data The array of chart data. |
| 111 |
* @param int $chart_id The chart id. |
| 112 |
* |
| 113 |
* @return array The array of filtered data. |
| 114 |
*/ |
| 115 |
public function filterChartData( $data, $chart_id ) { |
| 116 |
$source = $this->_getSource( $chart_id ); |
| 117 |
if ( ! $source ) { |
| 118 |
return $data; |
| 119 |
} |
| 120 |
|
| 121 |
return $source->repopulateData( $data, $chart_id ); |
| 122 |
} |
| 123 |
|
| 124 |
/** |
| 125 |
* Add the pro upsell html. |
| 126 |
* |
| 127 |
* @param string $old The previous html string. |
| 128 |
* @param string $feature What feature is this filter running for. |
| 129 |
* |
| 130 |
* @return string The new html code. |
| 131 |
*/ |
| 132 |
public function addProUpsell( $old, $feature = null, string $docs_url = '' ) { |
| 133 |
$pro_features = Visualizer_Module::get_features_for_license( 1 ); |
| 134 |
$biz_features = Visualizer_Module::get_features_for_license( 2 ); |
| 135 |
$return = ''; |
| 136 |
$feature = strval( $feature ); |
| 137 |
if ( empty( $feature ) || |
| 138 |
( in_array( $feature, $biz_features, true ) && ! apply_filters( 'visualizer_is_business', false ) ) || |
| 139 |
( in_array( $feature, $pro_features, true ) && ! Visualizer_Module::is_pro() ) |
| 140 |
) { |
| 141 |
// translators: $s - the name of the plan (PRO). |
| 142 |
$msg = sprintf( __( 'Upgrade to %s to activate this feature!', 'visualizer' ), 'PRO' ); |
| 143 |
// translators: $s - the name of the plan (Plus). |
| 144 |
$plus_msg = sprintf( __( 'Upgrade to %s plan to activate this feature!', 'visualizer' ), 'Plus' ); |
| 145 |
if ( in_array( $feature, $biz_features, true ) && Visualizer_Module::is_pro() ) { |
| 146 |
$msg = $plus_msg; |
| 147 |
} |
| 148 |
if ( in_array( $feature, array( 'db-query', 'chart-permissions' ), true ) ) { |
| 149 |
$msg = $plus_msg; |
| 150 |
} |
| 151 |
|
| 152 |
$return = '<div class="only-pro-content">'; |
| 153 |
$return .= ' <div class="only-pro-container">'; |
| 154 |
$return .= ' <div class="only-pro-inner">'; |
| 155 |
$return .= ' <p>' . $msg . '</p>'; |
| 156 |
if ( ! empty( $docs_url ) ) { |
| 157 |
$return .= ' <a target="_blank" href="' . esc_url( $docs_url ) . '" class="button button-secondary">' . esc_html__( 'Documentation', 'visualizer' ) . '</a>'; |
| 158 |
} |
| 159 |
$return .= ' <a target="_blank" href="' . tsdk_utmify( Visualizer_Plugin::PRO_TEASER_URL, esc_attr( $feature ) ) . '" title="' . __( 'Upgrade Now', 'visualizer' ) . '" class="button button-primary">' . __( 'Upgrade Now', 'visualizer' ) . '</a>'; |
| 160 |
$return .= ' </div>'; |
| 161 |
$return .= ' </div>'; |
| 162 |
$return .= '</div>'; |
| 163 |
} |
| 164 |
if ( empty( $feature ) && Visualizer_Module::is_pro() ) { |
| 165 |
remove_filter( 'visualizer_pro_upsell', 'addProUpsell', 10, 1 ); |
| 166 |
$return = ''; |
| 167 |
} |
| 168 |
|
| 169 |
return $return; |
| 170 |
} |
| 171 |
} |
| 172 |
|