PageRenderTime 54ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-content/plugins/wr-pagebuilder/shortcodes/pricing-table/pricing-table.php

https://gitlab.com/hunt9310/ras
PHP | 372 lines | 247 code | 48 blank | 77 comment | 36 complexity | 464e99773ec2ed444e0920c0101b42e3 MD5 | raw file
  1. <?php
  2. /**
  3. * @version $Id$
  4. * @package WR PageBuilder
  5. * @author WooRockets Team <support@woorockets.com>
  6. * @copyright Copyright (C) 2012 woorockets.com. All Rights Reserved.
  7. * @license GNU/GPL v2 or later http://www.gnu.org/licenses/gpl-2.0.html
  8. *
  9. * Websites: http://www.woorockets.com
  10. * Technical Support: Feedback - http://www.woorockets.com
  11. */
  12. if ( ! class_exists( 'WR_Pricing_Table' ) ) :
  13. /**
  14. * Pricing table element for WR PageBuilder.
  15. *
  16. * @since 1.0.0
  17. */
  18. class WR_Pricing_Table extends WR_Pb_Shortcode_Parent {
  19. /**
  20. * Constructor
  21. *
  22. * @return void
  23. */
  24. // Predefined Attributes
  25. static $attributes = array(
  26. 'max_domains' => array(
  27. 'title' => 'Max Domains',
  28. 'value' => array( '1', '5', '20' ),
  29. 'type' => 'text',
  30. ),
  31. 'storage' => array(
  32. 'title' => 'Storage',
  33. 'value' => array( '100 MB', '500 MB', '2 TB' ),
  34. 'type' => 'text',
  35. ),
  36. 'ssl_support' => array(
  37. 'title' => 'SSL Support',
  38. 'value' => array( 'no', 'yes', 'yes' ),
  39. 'type' => 'checkbox',
  40. ),
  41. );
  42. // Store index of pricing option/ pricing attribute
  43. static $index = 0;
  44. public function __construct() {
  45. parent::__construct();
  46. }
  47. /**
  48. * Configure shortcode.
  49. *
  50. * @return void
  51. */
  52. public function element_config() {
  53. $this->config['shortcode'] = strtolower( __CLASS__ );
  54. $this->config['name'] = __( 'Pricing Table', WR_PBL );
  55. $this->config['cat'] = __( 'Extra', WR_PBL );
  56. $this->config['icon'] = 'wr-icon-pricing-table';
  57. $this->config['description'] = __( 'Pricing table with flexible settings', WR_PBL );
  58. $this->config['has_subshortcode'] = 'WR_Item_' . str_replace( 'WR_', '', __CLASS__ );
  59. // Define exception for this shortcode
  60. $this->config['exception'] = array(
  61. 'default_content' => __( 'Pricing Table', WR_PBL ),
  62. 'data-modal-title' => __( 'Pricing Table', WR_PBL ),
  63. 'admin_assets' => array(
  64. // Shortcode style
  65. 'pricing-table.css',
  66. 'pricing_table.js',
  67. 'wr-linktype.js',
  68. 'wr-popover.js',
  69. 'item_pricing_table.js',
  70. ),
  71. 'frontend_assets' => array(
  72. // Bootstrap 3
  73. 'wr-pb-bootstrap-css',
  74. 'wr-pb-bootstrap-js',
  75. // Font IcoMoon
  76. 'wr-pb-font-icomoon-css',
  77. // Fancy Box
  78. 'wr-pb-jquery-fancybox-css',
  79. 'wr-pb-jquery-fancybox-js',
  80. // Shortcode style
  81. 'pricing-table_frontend.css',
  82. 'pricing_table_frontend.js',
  83. ),
  84. );
  85. // Use Ajax to speed up element settings modal loading speed
  86. $this->config['edit_using_ajax'] = true;
  87. add_action( 'wr_pb_modal_footer', array( &$this, '_modal_footer' ) );
  88. }
  89. /**
  90. * Define shortcode settings.
  91. *
  92. * @return void
  93. */
  94. public function element_items() {
  95. $this->items = array(
  96. 'content' => array(
  97. array(
  98. 'name' => __( 'Attributes', WR_PBL ),
  99. 'id' => 'prtbl_attr',
  100. 'type' => 'group',
  101. 'shortcode' => ucfirst( __CLASS__ ),
  102. 'sub_item_type' => $this->config['has_subshortcode'] . '_Attr',
  103. 'sub_items' => array(
  104. WR_Pricing_Table::get_option( 'max_domains' ),
  105. WR_Pricing_Table::get_option( 'storage' ),
  106. WR_Pricing_Table::get_option( 'ssl_support' ),
  107. ),
  108. 'overwrite_shortcode_data' => false,
  109. ),
  110. array(
  111. 'name' => __( 'Options', WR_PBL ),
  112. 'id' => 'prtbl_items',
  113. 'type' => 'group',
  114. 'no_title' => __( '(Untitled)', WR_PBL ),
  115. 'shortcode' => ucfirst( __CLASS__ ),
  116. 'sub_item_type' => $this->config['has_subshortcode'],
  117. 'sub_items' => array(
  118. array( 'std' => '', 'prtbl_item_title' => 'Free', 'prtbl_item_desc' => 'Free', 'prtbl_item_currency' => '$', 'prtbl_item_price' => '0', 'prtbl_item_time' => ' / month' ),
  119. array( 'std' => '', 'prtbl_item_title' => 'Standard', 'prtbl_item_desc' => 'Standard', 'prtbl_item_currency' => '$', 'prtbl_item_price' => '69', 'prtbl_item_feature' => 'yes', 'prtbl_item_time' => ' / month' ),
  120. array( 'std' => '', 'prtbl_item_title' => 'Premium', 'prtbl_item_desc' => 'Premium', 'prtbl_item_currency' => '$', 'prtbl_item_price' => '99', 'prtbl_item_time' => ' / month' ),
  121. ),
  122. 'overwrite_shortcode_data' => false,
  123. ),
  124. ),
  125. 'styling' => array(
  126. array(
  127. 'type' => 'preview',
  128. ),
  129. array(
  130. 'name' => __( 'Elements', WR_PBL ),
  131. 'id' => 'prtbl_elements',
  132. 'type' => 'checkbox',
  133. 'class' => 'jsn-column-item checkbox',
  134. 'container_class' => 'jsn-columns-container jsn-columns-count-two',
  135. 'std' => 'title__#__button__#__attributes',
  136. 'options' => array(
  137. 'title' => __( 'Title', WR_PBL ),
  138. 'description' => __( 'Description', WR_PBL ),
  139. 'image' => __( 'Image', WR_PBL ),
  140. 'attributes' => __( 'Attributes', WR_PBL ),
  141. 'price' => __( 'Price', WR_PBL ),
  142. 'button' => __( 'Button', WR_PBL )
  143. ),
  144. 'tooltip' => __( 'Elements to display on pricing table', WR_PBL )
  145. ),
  146. WR_Pb_Helper_Type::get_apprearing_animations(),
  147. WR_Pb_Helper_Type::get_animation_speeds(),
  148. )
  149. );
  150. }
  151. /**
  152. * Function to sync sub-shortcode content become sub-shortcode array
  153. *
  154. * @param array $arr_shortcodes
  155. */
  156. private function sync_sub_content( $sub_shortcode = '' ) {
  157. $arr_shortcodes = array();
  158. if ( ! $sub_shortcode )
  159. return;
  160. // Convert to sub-shortcode array
  161. $arr_sub_shortcode = $arr_values = array();
  162. $pattern = '\\[(\\[?)(wr_item_pricing_table_attr)(?![\\w-])([^\\]\\/]*(?:\\/(?!\\])[^\\]\\/]*)*?)(?:(\\/)\\]|\\](?:([^\\[]*+(?:\\[(?!\\/\\2\\])[^\\[]*+)*+)\\[\\/\\2\\])?)(\\]?)';
  163. preg_match_all( "/$pattern/s", $sub_shortcode, $matches );
  164. $arr_sub_shortcode['WR_Item_Pricing_Table_Attr'] = $matches[0];
  165. if ( isset( $arr_sub_shortcode['WR_Item_Pricing_Table_Attr'] ) && is_array( $arr_sub_shortcode['WR_Item_Pricing_Table_Attr'] ) ) {
  166. //$str_pr_tbl_shortcode = implode( '', $arr_sub_shortcode['WR_Item_Pricing_Table_Attr'] );
  167. $arr_shortcodes['wr_item_pricing_table_attr'] = implode( '', $arr_sub_shortcode['WR_Item_Pricing_Table_Attr'] );
  168. }
  169. $pattern = '\\[(\\[?)(wr_item_pricing_table)(?![\\w-])([^\\]\\/]*(?:\\/(?!\\])[^\\]\\/]*)*?)(?:(\\/)\\]|\\](?:([^\\[]*+(?:\\[(?!\\/\\2\\])[^\\[]*+)*+)\\[\\/\\2\\])?)(\\]?)';
  170. preg_match_all( "/$pattern/s", $sub_shortcode, $matches );
  171. $arr_sub_shortcode['WR_Item_Pricing_Table'] = $matches[0];
  172. if ( isset( $arr_sub_shortcode['WR_Item_Pricing_Table'] ) && is_array( $arr_sub_shortcode['WR_Item_Pricing_Table'] ) ) {
  173. foreach ( $arr_sub_shortcode['WR_Item_Pricing_Table'] as $i => $item ) {
  174. $pattern = '\\[(\\[?)(wr_item_pricing_table_attr_value)(?![\\w-])([^\\]\\/]*(?:\\/(?!\\])[^\\]\\/]*)*?)(?:(\\/)\\]|\\](?:([^\\[]*+(?:\\[(?!\\/\\2\\])[^\\[]*+)*+)\\[\\/\\2\\])?)(\\]?)';
  175. preg_match_all( "/$pattern/s", $item, $matches );
  176. $arr_values['WR_Item_Pricing_Table_Attr_Value'] = $matches[0];
  177. $count = count( $arr_values['WR_Item_Pricing_Table_Attr_Value'] );
  178. $_item = preg_replace( "/$pattern/s", '<!--wr-replace-flag-->', $item );
  179. // Simulate mechanism process sub-shortcode in modal template
  180. $sub_sc_data = WR_Item_Pricing_Table::_sub_items_filter( $arr_values, 'wr_item_pricing_table', $arr_sub_shortcode['WR_Item_Pricing_Table_Attr'] );
  181. if ( isset( $sub_sc_data['WR_Item_Pricing_Table_Attr_Value'] ) && is_array( $sub_sc_data['WR_Item_Pricing_Table_Attr_Value'] ) ) {
  182. $str_pr_tbl_shortcode = str_replace( str_repeat( '<!--wr-replace-flag-->', $count ), implode( '', $sub_sc_data['WR_Item_Pricing_Table_Attr_Value'] ), $_item );
  183. }
  184. $str_pr_tbl_shortcode = str_replace( '"prtbl_item_attr_value', '" prtbl_item_attr_value', $str_pr_tbl_shortcode );
  185. $arr_shortcodes['wr_item_pricing_table'][] = $str_pr_tbl_shortcode;
  186. }
  187. }
  188. return $arr_shortcodes;
  189. }
  190. private function check_field_allow( $allow = '', $pattern_scan = '', $arr_allows, $pr_tbl_col_value_html = '' ) {
  191. if ( ! $allow || ! $pattern_scan || ! is_array( $arr_allows ) || ! $pr_tbl_col_value_html )
  192. return $pr_tbl_col_value_html;
  193. if ( in_array( $allow, $arr_allows ) ) {
  194. $pr_tbl_col_value_html = str_replace( "[$pattern_scan]", '', $pr_tbl_col_value_html );
  195. $pr_tbl_col_value_html = str_replace( "[/$pattern_scan]", '', $pr_tbl_col_value_html );
  196. } else {
  197. $pattern = '\\[(\\[?)('. $pattern_scan .')(?![\\w-])([^\\]\\/]*(?:\\/(?!\\])[^\\]\\/]*)*?)(?:(\\/)\\]|\\](?:([^\\[]*+(?:\\[(?!\\/\\2\\])[^\\[]*+)*+)\\[\\/\\2\\])?)(\\]?)';
  198. $pr_tbl_col_value_html = preg_replace( "/$pattern/s", '', $pr_tbl_col_value_html );
  199. }
  200. return $pr_tbl_col_value_html;
  201. }
  202. /**
  203. * Generate HTML code from shortcode content.
  204. *
  205. * @param array $atts Shortcode attributes.
  206. * @param string $content Current content.
  207. *
  208. * @return string
  209. */
  210. public function element_shortcode_full( $atts = null, $content = null ) {
  211. $html_element = '';
  212. $arr_sub_shortcodes = self::sync_sub_content( $content );
  213. $arr_params = ( shortcode_atts( $this->config['params'], $atts ) );
  214. extract( $arr_params );
  215. $arr_elements = explode( '__#__', $prtbl_elements );
  216. // Build html for cols label.
  217. $pr_tbl_label_html = '<div class="wr-prtbl-cols first">';
  218. // Append blank header
  219. $header_ = '<div class="wr-prtbl-title">';
  220. if ( in_array( 'image', $arr_elements ) ) {
  221. $header_ .= '<div class="wr-prtbl-image"></div>';
  222. }
  223. $header_ .= '<h3>&nbsp;</h3></div>';
  224. if ( in_array( 'price', $arr_elements ) || in_array( 'description', $arr_elements ) ) {
  225. $header_ .= '<div class="wr-prtbl-meta">';
  226. // append blank price
  227. if ( in_array( 'price', $arr_elements ) ) {
  228. $header_ .= '<div class="wr-prtbl-price">&nbsp;</div>';
  229. }
  230. // append blank price
  231. if ( in_array( 'description', $arr_elements ) ) {
  232. $header_ .= '<p class="wr-prtbl-desc">&nbsp;</p>';
  233. }
  234. $header_ .= '</div>';
  235. }
  236. // Process deactive pricing table item attribute
  237. set_transient( 'pricingtable_attrs' , '' );
  238. $pattern = '\\[(\\[?)(wr_item_pricing_table_attr)(?![\\w-])([^\\]\\/]*(?:\\/(?!\\])[^\\]\\/]*)*?)(?:(\\/)\\]|\\](?:([^\\[]*+(?:\\[(?!\\/\\2\\])[^\\[]*+)*+)\\[\\/\\2\\])?)(\\]?)';
  239. preg_match_all( "/$pattern/s", $arr_sub_shortcodes['wr_item_pricing_table_attr'], $matches );
  240. $arr_prtbl_atts = $matches[0];
  241. $arr_disable_el = array();
  242. foreach ( $arr_prtbl_atts as $i => $att ) {
  243. if ( ! empty( $att ) ) {
  244. $att_extract_params = WR_Pb_Helper_Shortcode::extract_params( $att );
  245. if ( isset( $att_extract_params['disabled_el'] ) && $att_extract_params['disabled_el'] == 'yes' && isset( $att_extract_params['prtbl_item_attr_id'] ) ) {
  246. $arr_disable_el[] = $att_extract_params['prtbl_item_attr_id'];
  247. }
  248. }
  249. }
  250. set_transient( 'pricingtable_attrs' , json_encode( $arr_disable_el ) );
  251. $pr_tbl_label_html .= sprintf( '<div class="wr-prtbl-header">%s</div>', balanceTags( $header_ ) );
  252. if ( isset( $arr_sub_shortcodes['wr_item_pricing_table_attr'] ) && ! empty( $arr_sub_shortcodes['wr_item_pricing_table_attr'] ) ) {
  253. $pr_tbl_label_html .= '<ul class="wr-prtbl-features">';
  254. $pr_tbl_label_html .= do_shortcode( $arr_sub_shortcodes['wr_item_pricing_table_attr'] );
  255. $pr_tbl_label_html .= '</ul>';
  256. }
  257. $pr_tbl_label_html .= '<div class="wr-prtbl-footer"></div>';
  258. $pr_tbl_label_html .= '</div>';
  259. // Build html for cols value.
  260. $pr_tbl_col_value_html = '';
  261. if ( isset( $arr_sub_shortcodes['wr_item_pricing_table'] ) && ! empty( $arr_sub_shortcodes['wr_item_pricing_table'] ) ) {
  262. $pr_tbl_col_value_html = do_shortcode( implode( '', $arr_sub_shortcodes['wr_item_pricing_table'] ) );
  263. $pr_tbl_col_value_html = $this->check_field_allow( 'title', 'prtbl_item_title', $arr_elements, $pr_tbl_col_value_html );
  264. $pr_tbl_col_value_html = $this->check_field_allow( 'description', 'prtbl_item_desc', $arr_elements, $pr_tbl_col_value_html );
  265. $pr_tbl_col_value_html = $this->check_field_allow( 'image', 'prtbl_item_image', $arr_elements, $pr_tbl_col_value_html );
  266. $pr_tbl_col_value_html = $this->check_field_allow( 'button', 'prtbl_item_button', $arr_elements, $pr_tbl_col_value_html );
  267. $pr_tbl_col_value_html = $this->check_field_allow( 'price', 'prtbl_item_price', $arr_elements, $pr_tbl_col_value_html );
  268. if ( ! in_array( 'price', $arr_elements ) && ! in_array( 'description', $arr_elements ) ) {
  269. $pattern = '\\[(\\[?)(prtbl_item_meta)(?![\\w-])([^\\]\\/]*(?:\\/(?!\\])[^\\]\\/]*)*?)(?:(\\/)\\]|\\](?:([^\\[]*+(?:\\[(?!\\/\\2\\])[^\\[]*+)*+)\\[\\/\\2\\])?)(\\]?)';
  270. $pr_tbl_col_value_html = preg_replace( "/$pattern/s", '', $pr_tbl_col_value_html );
  271. } else {
  272. $pr_tbl_col_value_html = str_replace( "[prtbl_item_meta]", '', $pr_tbl_col_value_html );
  273. $pr_tbl_col_value_html = str_replace( "[/prtbl_item_meta]", '', $pr_tbl_col_value_html );
  274. }
  275. }
  276. $count_columns = isset( $arr_sub_shortcodes['wr_item_pricing_table'] ) ? count( $arr_sub_shortcodes['wr_item_pricing_table'] ) + 1 : 1;
  277. $html_element = $pr_tbl_label_html . $pr_tbl_col_value_html;
  278. return $this->element_wrapper( $html_element, $arr_params, "table-$count_columns-col" );
  279. }
  280. /**
  281. * Get shortcode parameters for Pricing Option
  282. *
  283. * @param string $attribute The ID of attribute
  284. * @param bool $include_value Whether or not including Value parameter (true if call for WR_Item_Pricing_Table_Attr_Value)
  285. *
  286. * @return string
  287. */
  288. static function get_option( $attribute, $include_value = false ) {
  289. // get all Predefined Attributes
  290. $attributes = WR_Pricing_Table::$attributes;
  291. // get index of current Option/Attribute
  292. $idx = WR_Pricing_Table::$index = WR_Pricing_Table::$index % 3;
  293. $title = isset ( $attributes[$attribute] ) ? ( isset ( $attributes[$attribute]['title'] ) ? $attributes[$attribute]['title'] : '' ) : '';
  294. $type = isset ( $attributes[$attribute] ) ? ( isset ( $attributes[$attribute]['type'] ) ? $attributes[$attribute]['type'] : '' ) : '';
  295. if ( $include_value ) {
  296. $value = isset ( $attributes[$attribute] ) ? ( isset ( $attributes[$attribute]['value'][$idx] ) ? $attributes[$attribute]['value'][$idx] : '' ) : '';
  297. }
  298. $result = array(
  299. 'std' => '', 'prtbl_item_attr_id' => $attribute, 'prtbl_item_attr_title' => $title, 'prtbl_item_attr_type' => $type,
  300. );
  301. if ( ! $include_value ) {
  302. $result['prtbl_item_attr_desc'] = $title;
  303. } else {
  304. $result['prtbl_item_attr_value'] = $result['prtbl_item_attr_desc'] = $value;
  305. }
  306. return $result;
  307. }
  308. /**
  309. * Print Setting HTML of Pricing Item
  310. */
  311. function _modal_footer( $shortcode ) {
  312. $pricing_item = new WR_Item_Pricing_Table();
  313. if ( $shortcode == $this->config['shortcode'] ) {
  314. // Create instance of sub shortcode
  315. $instance = new $pricing_item->config['has_subshortcode'];
  316. $instance->init_element();
  317. // Get modal settings html
  318. $settings_html = WR_Pb_Objects_Modal::get_shortcode_modal_settings( $instance->items, $instance->config['shortcode'] );
  319. // Print html as template script
  320. printf( "<script type='text/html' id='tmpl-wr-pb-hidden-setting'>\n%s\n</script>\n", balanceTags( $settings_html ) );
  321. }
  322. }
  323. }
  324. endif;