/inc/core/generator.php

  1. <?php 
  2. /** 
  3. * Shortcode Generator 
  4. */ 
  5. class Su_Generator { 
  6.  
  7. /** 
  8. * Constructor 
  9. */ 
  10. function __construct() { 
  11. add_action( 'media_buttons', array( __CLASS__, 'button' ), 1000 ); 
  12.  
  13. add_action( 'su/update', array( __CLASS__, 'reset' ) ); 
  14. add_action( 'su/activation', array( __CLASS__, 'reset' ) ); 
  15. add_action( 'sunrise/page/before', array( __CLASS__, 'reset' ) ); 
  16. add_action( 'create_term', array( __CLASS__, 'reset' ), 10, 3 ); 
  17. add_action( 'edit_term', array( __CLASS__, 'reset' ), 10, 3 ); 
  18. add_action( 'delete_term', array( __CLASS__, 'reset' ), 10, 3 ); 
  19.  
  20. add_action( 'wp_ajax_su_generator_settings', array( __CLASS__, 'settings' ) ); 
  21. add_action( 'wp_ajax_su_generator_preview', array( __CLASS__, 'preview' ) ); 
  22. add_action( 'su/generator/actions', array( __CLASS__, 'presets' ) ); 
  23.  
  24. add_action( 'wp_ajax_su_generator_get_icons', array( __CLASS__, 'ajax_get_icons' ) ); 
  25. add_action( 'wp_ajax_su_generator_get_terms', array( __CLASS__, 'ajax_get_terms' ) ); 
  26. add_action( 'wp_ajax_su_generator_get_taxonomies', array( __CLASS__, 'ajax_get_taxonomies' ) ); 
  27. add_action( 'wp_ajax_su_generator_add_preset', array( __CLASS__, 'ajax_add_preset' ) ); 
  28. add_action( 'wp_ajax_su_generator_remove_preset', array( __CLASS__, 'ajax_remove_preset' ) ); 
  29. add_action( 'wp_ajax_su_generator_get_preset', array( __CLASS__, 'ajax_get_preset' ) ); 
  30.  
  31. /** 
  32. * Generator button 
  33. */ 
  34. public static function button( $args = array() ) { 
  35. // Check access 
  36. if ( !self::access_check() ) return; 
  37. // Prepare button target 
  38. $target = is_string( $args ) ? $args : 'content'; 
  39. // Prepare args 
  40. $args = wp_parse_args( $args, array( 
  41. 'target' => $target,  
  42. 'text' => __( 'Insert shortcode', 'shortcodes-ultimate' ),  
  43. 'class' => 'button',  
  44. 'icon' => plugins_url( 'assets/images/icon.png', SU_PLUGIN_FILE ),  
  45. 'echo' => true,  
  46. 'shortcode' => false 
  47. ) ); 
  48. // Prepare icon 
  49. if ( $args['icon'] ) $args['icon'] = '<img src="' . $args['icon'] . '" /> '; 
  50. // Print button 
  51. $button = '<a href="javascript:void(0);" class="su-generator-button ' . $args['class'] . '" title="' . $args['text'] . '" data-target="' . $args['target'] . '" data-mfp-src="#su-generator" data-shortcode="' . (string) $args['shortcode'] . '">' . $args['icon'] . $args['text'] . '</a>'; 
  52. // Show generator popup 
  53. add_action( 'wp_footer', array( __CLASS__, 'popup' ) ); 
  54. add_action( 'admin_footer', array( __CLASS__, 'popup' ) ); 
  55. // Request assets 
  56. wp_enqueue_media(); 
  57. su_query_asset( 'css', array( 'simpleslider', 'farbtastic', 'magnific-popup', 'font-awesome', 'su-generator' ) ); 
  58. su_query_asset( 'js', array( 'jquery', 'jquery-ui-core', 'jquery-ui-widget', 'jquery-ui-mouse', 'simpleslider', 'farbtastic', 'magnific-popup', 'jquery-hotkeys', 'su-generator' ) ); 
  59. // Hook 
  60. do_action( 'su/button', $args ); 
  61. // Print/return result 
  62. if ( $args['echo'] ) echo $button; 
  63. return $button; 
  64.  
  65. /** 
  66. * Cache reset 
  67. */ 
  68. public static function reset() { 
  69. // Clear popup cache 
  70. delete_transient( 'su/generator/popup' ); 
  71. // Clear shortcodes settings cache 
  72. foreach ( array_keys( (array) Su_Data::shortcodes() ) as $shortcode ) delete_transient( 'su/generator/settings/' . $shortcode ); 
  73.  
  74. /** 
  75. * Generator popup form 
  76. */ 
  77. public static function popup() { 
  78. // Get cache 
  79. $output = get_transient( 'su/generator/popup' ); 
  80. if ( $output && SU_ENABLE_CACHE ) echo $output; 
  81. // Cache not found 
  82. else { 
  83. ob_start(); 
  84. $tools = apply_filters( 'su/generator/tools', array( 
  85. '<a href="' . admin_url( 'admin.php?page=shortcodes-ultimate' ) . '#tab-1" target="_blank" title="' . __( 'Settings', 'shortcodes-ultimate' ) . '">' . __( 'Plugin settings', 'shortcodes-ultimate' ) . '</a>',  
  86. '<a href="http://gndev.info/shortcodes-ultimate/" target="_blank" title="' . __( 'Plugin homepage', 'shortcodes-ultimate' ) . '">' . __( 'Plugin homepage', 'shortcodes-ultimate' ) . '</a>',  
  87. '<a href="http://wordpress.org/support/plugin/shortcodes-ultimate/" target="_blank" title="' . __( 'Support forums', 'shortcodes-ultimate' ) . '">' . __( 'Support forums', 'shortcodes-ultimate' ) . '</a>' 
  88. ) ); 
  89.  
  90. // Add add-ons links 
  91. if ( !su_addon_active( 'maker' ) || !su_addon_active( 'skins' ) || !su_addon_active( 'extra' ) ) $tools[] = '<a href="' . admin_url( 'admin.php?page=shortcodes-ultimate-addons' ) . '" target="_blank" title="' . __( 'Add-ons', 'shortcodes-ultimate' ) . '" class="su-add-ons">' . __( 'Add-ons', 'shortcodes-ultimate' ) . '</a>'; 
  92. ?> 
  93. <div id="su-generator-wrap" style="display:none"> 
  94. <div id="su-generator"> 
  95. <div id="su-generator-header"> 
  96. <div id="su-generator-tools"><?php echo implode( ' <span></span> ', $tools ); ?></div> 
  97. <input type="text" name="su_generator_search" id="su-generator-search" value="" placeholder="<?php _e( 'Search for shortcodes', 'shortcodes-ultimate' ); ?>" /> 
  98. <p id="su-generator-search-pro-tip"><?php printf( '<strong>%s:</strong> %s', __( 'Pro Tip', 'shortcodes-ultimate' ), __( 'Hit enter to select highlighted shortcode, while searching' ) ) ?></p> 
  99. <div id="su-generator-filter"> 
  100. <strong><?php _e( 'Filter by type', 'shortcodes-ultimate' ); ?></strong> 
  101. <?php foreach ( (array) Su_Data::groups() as $group => $label ) echo '<a href="#" data-filter="' . $group . '">' . $label . '</a>'; ?> 
  102. </div> 
  103. <div id="su-generator-choices" class="su-generator-clearfix"> 
  104. <?php 
  105. // Choices loop 
  106. foreach ( (array) Su_Data::shortcodes() as $name => $shortcode ) { 
  107. $icon = ( isset( $shortcode['icon'] ) ) ? $shortcode['icon'] : 'puzzle-piece'; 
  108. $shortcode['name'] = ( isset( $shortcode['name'] ) ) ? $shortcode['name'] : $name; 
  109. echo '<span data-name="' . $shortcode['name'] . '" data-shortcode="' . $name . '" title="' . esc_attr( $shortcode['desc'] ) . '" data-desc="' . esc_attr( $shortcode['desc'] ) . '" data-group="' . $shortcode['group'] . '">' . Su_Tools::icon( $icon ) . $shortcode['name'] . '</span>' . "\n"; 
  110. ?> 
  111. </div> 
  112. </div> 
  113. <div id="su-generator-settings"></div> 
  114. <input type="hidden" name="su-generator-selected" id="su-generator-selected" value="<?php echo plugins_url( '', SU_PLUGIN_FILE ); ?>" /> 
  115. <input type="hidden" name="su-generator-url" id="su-generator-url" value="<?php echo plugins_url( '', SU_PLUGIN_FILE ); ?>" /> 
  116. <input type="hidden" name="su-compatibility-mode-prefix" id="su-compatibility-mode-prefix" value="<?php echo su_compatibility_mode_prefix(); ?>" /> 
  117. <div id="su-generator-result" style="display:none"></div> 
  118. </div> 
  119. </div> 
  120. <?php 
  121. $output = ob_get_contents(); 
  122. set_transient( 'su/generator/popup', $output, 2 * DAY_IN_SECONDS ); 
  123. ob_end_clean(); 
  124. echo $output; 
  125.  
  126. /** 
  127. * Process AJAX request 
  128. */ 
  129. public static function settings() { 
  130. self::access(); 
  131. // Param check 
  132. if ( empty( $_REQUEST['shortcode'] ) ) wp_die( __( 'Shortcode not specified', 'shortcodes-ultimate' ) ); 
  133. // Get cache 
  134. $output = get_transient( 'su/generator/settings/' . sanitize_text_field( $_REQUEST['shortcode'] ) ); 
  135. if ( $output && SU_ENABLE_CACHE ) echo $output; 
  136. // Cache not found 
  137. else { 
  138. // Request queried shortcode 
  139. $shortcode = Su_Data::shortcodes( sanitize_key( $_REQUEST['shortcode'] ) ); 
  140. // Prepare skip-if-default option 
  141. $skip = ( get_option( 'su_option_skip' ) === 'on' ) ? ' su-generator-skip' : ''; 
  142. // Prepare actions 
  143. $actions = apply_filters( 'su/generator/actions', array( 
  144. 'insert' => '<a href="javascript:void(0);" class="button button-primary button-large su-generator-insert"><i class="fa fa-check"></i> ' . __( 'Insert shortcode', 'shortcodes-ultimate' ) . '</a>',  
  145. 'preview' => '<a href="javascript:void(0);" class="button button-large su-generator-toggle-preview"><i class="fa fa-eye"></i> ' . __( 'Live preview', 'shortcodes-ultimate' ) . '</a>' 
  146. ) ); 
  147. // Shortcode header 
  148. $return = '<div id="su-generator-breadcrumbs">'; 
  149. $return .= apply_filters( 'su/generator/breadcrumbs', '<a href="javascript:void(0);" class="su-generator-home" title="' . __( 'Click to return to the shortcodes list', 'shortcodes-ultimate' ) . '">' . __( 'All shortcodes', 'shortcodes-ultimate' ) . '</a> → <span>' . $shortcode['name'] . '</span> <small class="alignright">' . $shortcode['desc'] . '</small><div class="su-generator-clear"></div>' ); 
  150. $return .= '</div>'; 
  151. // Shortcode note 
  152. if ( isset( $shortcode['note'] ) || isset( $shortcode['example'] ) ) { 
  153. $return .= '<div class="su-generator-note"><i class="fa fa-info-circle"></i><div class="su-generator-note-content">'; 
  154. if ( isset( $shortcode['note'] ) ) $return .= wpautop( $shortcode['note'] ); 
  155. if ( isset( $shortcode['example'] ) ) $return .= wpautop( '<a href="' . admin_url( 'admin.php?page=shortcodes-ultimate-examples&example=' . $shortcode['example'] ) . '" target="_blank">' . __( 'Examples of use', 'shortcodes-ultimate' ) . ' →</a>' ); 
  156. $return .= '</div></div>'; 
  157. // Shortcode has atts 
  158. if ( isset( $shortcode['atts'] ) && count( $shortcode['atts'] ) ) { 
  159. // Loop through shortcode parameters 
  160. foreach ( $shortcode['atts'] as $attr_name => $attr_info ) { 
  161. // Prepare default value 
  162. $default = (string) ( isset( $attr_info['default'] ) ) ? $attr_info['default'] : ''; 
  163. $attr_info['name'] = ( isset( $attr_info['name'] ) ) ? $attr_info['name'] : $attr_name; 
  164. $return .= '<div class="su-generator-attr-container' . $skip . '" data-default="' . esc_attr( $default ) . '">'; 
  165. $return .= '<h5>' . $attr_info['name'] . '</h5>'; 
  166. // Create field types 
  167. if ( !isset( $attr_info['type'] ) && isset( $attr_info['values'] ) && is_array( $attr_info['values'] ) && count( $attr_info['values'] ) ) $attr_info['type'] = 'select'; 
  168. elseif ( !isset( $attr_info['type'] ) ) $attr_info['type'] = 'text'; 
  169. if ( is_callable( array( 'Su_Generator_Views', $attr_info['type'] ) ) ) $return .= call_user_func( array( 'Su_Generator_Views', $attr_info['type'] ), $attr_name, $attr_info ); 
  170. elseif ( isset( $attr_info['callback'] ) && is_callable( $attr_info['callback'] ) ) $return .= call_user_func( $attr_info['callback'], $attr_name, $attr_info ); 
  171. if ( isset( $attr_info['desc'] ) ) $attr_info['desc'] = str_replace( '%su_skins_link%', su_skins_link(), $attr_info['desc'] ); 
  172. if ( isset( $attr_info['desc'] ) ) $return .= '<div class="su-generator-attr-desc">' . str_replace( array( '<b%value>', '<b_>' ), '<b class="su-generator-set-value" title="' . __( 'Click to set this value', 'shortcodes-ultimate' ) . '">', $attr_info['desc'] ) . '</div>'; 
  173. $return .= '</div>'; 
  174. // Single shortcode (not closed) 
  175. if ( $shortcode['type'] == 'single' ) $return .= '<input type="hidden" name="su-generator-content" id="su-generator-content" value="false" />'; 
  176. // Wrapping shortcode 
  177. else { 
  178. // Prepare shortcode content 
  179. $shortcode['content'] = ( isset( $shortcode['content'] ) ) ? $shortcode['content'] : ''; 
  180. $return .= '<div class="su-generator-attr-container"><h5>' . __( 'Content', 'shortcodes-ultimate' ) . '</h5><textarea name="su-generator-content" id="su-generator-content" rows="5">' . esc_attr( str_replace( array( '%prefix_', '__' ), su_cmpt(), $shortcode['content'] ) ) . '</textarea></div>'; 
  181. $return .= '<div id="su-generator-preview"></div>'; 
  182. $return .= '<div class="su-generator-actions su-generator-clearfix">' . implode( ' ', array_values( $actions ) ) . '</div>'; 
  183. set_transient( 'su/generator/settings/' . sanitize_text_field( $_REQUEST['shortcode'] ), $return, 2 * DAY_IN_SECONDS ); 
  184. echo $return; 
  185. exit; 
  186.  
  187. /** 
  188. * Process AJAX request and generate preview HTML 
  189. */ 
  190. public static function preview() { 
  191. // Check authentication 
  192. self::access(); 
  193. // Output results 
  194. do_action( 'su/generator/preview/before' ); 
  195. echo '<h5>' . __( 'Preview', 'shortcodes-ultimate' ) . '</h5>'; 
  196. // echo '<hr />' . stripslashes( $_POST['shortcode'] ) . '<hr />'; // Uncomment for debug 
  197. echo do_shortcode( str_replace( '\"', '"', $_POST['shortcode'] ) ); 
  198. echo '<div style="clear:both"></div>'; 
  199. do_action( 'su/generator/preview/after' ); 
  200. die(); 
  201.  
  202. public static function access() { 
  203. if ( !self::access_check() ) wp_die( __( 'Access denied', 'shortcodes-ultimate' ) ); 
  204.  
  205. public static function access_check() { 
  206. $by_role = ( get_option( 'su_generator_access' ) ) ? current_user_can( get_option( 'su_generator_access' ) ) : true; 
  207. return current_user_can( 'edit_posts' ) && $by_role; 
  208.  
  209. public static function ajax_get_icons() { 
  210. self::access(); 
  211. die( Su_Tools::icons() ); 
  212.  
  213. public static function ajax_get_terms() { 
  214. self::access(); 
  215. $args = array(); 
  216. if ( isset( $_REQUEST['tax'] ) ) $args['options'] = (array) Su_Tools::get_terms( sanitize_key( $_REQUEST['tax'] ) ); 
  217. if ( isset( $_REQUEST['class'] ) ) $args['class'] = (string) sanitize_key( $_REQUEST['class'] ); 
  218. if ( isset( $_REQUEST['multiple'] ) ) $args['multiple'] = (bool) sanitize_key( $_REQUEST['multiple'] ); 
  219. if ( isset( $_REQUEST['size'] ) ) $args['size'] = (int) sanitize_key( $_REQUEST['size'] ); 
  220. if ( isset( $_REQUEST['noselect'] ) ) $args['noselect'] = (bool) sanitize_key( $_REQUEST['noselect'] ); 
  221. die( Su_Tools::select( $args ) ); 
  222.  
  223. public static function ajax_get_taxonomies() { 
  224. self::access(); 
  225. $args = array(); 
  226. $args['options'] = Su_Tools::get_taxonomies(); 
  227. die( Su_Tools::select( $args ) ); 
  228.  
  229. public static function presets( $actions ) { 
  230. ob_start(); 
  231. ?> 
  232. <div class="su-generator-presets alignright" data-shortcode="<?php echo sanitize_key( $_REQUEST['shortcode'] ); ?>"> 
  233. <a href="javascript:void(0);" class="button button-large su-gp-button"><i class="fa fa-bars"></i> <?php _e( 'Presets', 'shortcodes-ultimate' ); ?></a> 
  234. <div class="su-gp-popup"> 
  235. <div class="su-gp-head"> 
  236. <a href="javascript:void(0);" class="button button-small button-primary su-gp-new"><?php _e( 'Save current settings as preset', 'shortcodes-ultimate' ); ?></a> 
  237. </div> 
  238. <div class="su-gp-list"> 
  239. <?php self::presets_list(); ?> 
  240. </div> 
  241. </div> 
  242. </div> 
  243. <?php 
  244. $actions['presets'] = ob_get_contents(); 
  245. ob_end_clean(); 
  246. return $actions; 
  247.  
  248. public static function presets_list( $shortcode = false ) { 
  249. // Shortcode isn't specified, try to get it from $_REQUEST 
  250. if ( !$shortcode ) $shortcode = $_REQUEST['shortcode']; 
  251. // Shortcode name is still doesn't exists, exit 
  252. if ( !$shortcode ) return; 
  253. // Shortcode has been specified, sanitize it 
  254. $shortcode = sanitize_key( $shortcode ); 
  255. // Get presets 
  256. $presets = get_option( 'su_presets_' . $shortcode ); 
  257. // Presets has been found 
  258. if ( is_array( $presets ) && count( $presets ) ) { 
  259. // Print the presets 
  260. foreach ( $presets as $preset ) { 
  261. echo '<span data-id="' . $preset['id'] . '"><em>' . stripslashes( $preset['name'] ) . '</em> <i class="fa fa-times"></i></span>'; 
  262. // Hide default text 
  263. echo sprintf( '<b style="display:none">%s</b>', __( 'Presets not found', 'shortcodes-ultimate' ) ); 
  264. // Presets doesn't found 
  265. else echo sprintf( '<b>%s</b>', __( 'Presets not found', 'shortcodes-ultimate' ) ); 
  266.  
  267. public static function ajax_add_preset() { 
  268. self::access(); 
  269. // Check incoming data 
  270. if ( empty( $_POST['id'] ) ) return; 
  271. if ( empty( $_POST['name'] ) ) return; 
  272. if ( empty( $_POST['settings'] ) ) return; 
  273. if ( empty( $_POST['shortcode'] ) ) return; 
  274. // Clean-up incoming data 
  275. $id = sanitize_key( $_POST['id'] ); 
  276. $name = sanitize_text_field( $_POST['name'] ); 
  277. $settings = ( is_array( $_POST['settings'] ) ) ? stripslashes_deep( $_POST['settings'] ) : array(); 
  278. $shortcode = sanitize_key( $_POST['shortcode'] ); 
  279. // Prepare option name 
  280. $option = 'su_presets_' . $shortcode; 
  281. // Get the existing presets 
  282. $current = get_option( $option ); 
  283. // Create array with new preset 
  284. $new = array( 
  285. 'id' => $id,  
  286. 'name' => $name,  
  287. 'settings' => $settings 
  288. ); 
  289. // Add new array to the option value 
  290. if ( !is_array( $current ) ) $current = array(); 
  291. $current[$id] = $new; 
  292. // Save updated option 
  293. update_option( $option, $current ); 
  294. // Clear cache 
  295. delete_transient( 'su/generator/settings/' . $shortcode ); 
  296.  
  297. public static function ajax_remove_preset() { 
  298. self::access(); 
  299. // Check incoming data 
  300. if ( empty( $_POST['id'] ) ) return; 
  301. if ( empty( $_POST['shortcode'] ) ) return; 
  302. // Clean-up incoming data 
  303. $id = sanitize_key( $_POST['id'] ); 
  304. $shortcode = sanitize_key( $_POST['shortcode'] ); 
  305. // Prepare option name 
  306. $option = 'su_presets_' . $shortcode; 
  307. // Get the existing presets 
  308. $current = get_option( $option ); 
  309. // Check that preset is exists 
  310. if ( !is_array( $current ) || empty( $current[$id] ) ) return; 
  311. // Remove preset 
  312. unset( $current[$id] ); 
  313. // Save updated option 
  314. update_option( $option, $current ); 
  315. // Clear cache 
  316. delete_transient( 'su/generator/settings/' . $shortcode ); 
  317.  
  318. public static function ajax_get_preset() { 
  319. self::access(); 
  320. // Check incoming data 
  321. if ( empty( $_GET['id'] ) ) return; 
  322. if ( empty( $_GET['shortcode'] ) ) return; 
  323. // Clean-up incoming data 
  324. $id = sanitize_key( $_GET['id'] ); 
  325. $shortcode = sanitize_key( $_GET['shortcode'] ); 
  326. // Default data 
  327. $data = array(); 
  328. // Get the existing presets 
  329. $presets = get_option( 'su_presets_' . $shortcode ); 
  330. // Check that preset is exists 
  331. if ( is_array( $presets ) && isset( $presets[$id]['settings'] ) ) $data = $presets[$id]['settings']; 
  332. // Print results 
  333. die( json_encode( $data ) ); 
  334.  
  335. new Su_Generator; 
  336.  
  337. class Shortcodes_Ultimate_Generator extends Su_Generator { 
  338. function __construct() { 
  339. parent::__construct(); 
.