, Ruben Garcia * @since 1.0.0 */ // Exit if accessed directly if( !defined( 'ABSPATH' ) ) exit; class GamiPress_Shortcodes_Editor { /** * @var array $shortcodes All registered shortcodes * @since 1.4.7 */ public $shortcodes = array(); /** * @var bool $button_rendered Flag to check if button has been rendered * @since 1.4.7 */ public $button_rendered = false; /** * @var bool $rendering_shortcodes Flag to check if currently is rendering shortcodes * @since 1.4.7.1 */ public $rendering_shortcodes = false; public function __construct() { $this->shortcodes = gamipress_get_shortcodes(); add_action( 'admin_enqueue_scripts', array( $this, 'admin_scripts' ), 99 ); add_action( 'media_buttons', array( $this, 'render_button'), 20 ); add_action( 'admin_footer', array( $this, 'render_modal' ) ); } /** * Enqueue and localize relevant admin scripts * * @since 1.0.0 */ public function admin_scripts( $hook ) { $min = defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ? '' : '.min'; // Enqueue shortcodes editor wp_enqueue_script( 'gamipress-shortcodes-editor', GAMIPRESS_URL . 'assets/js/gamipress-shortcodes-editor' . $min . '.js', array( 'jquery', 'gamipress-admin-functions-js', 'gamipress-select2-js' ), GAMIPRESS_VER, true ); wp_localize_script( 'gamipress-shortcodes-editor', 'gamipress_shortcodes_editor', array( 'nonce' => gamipress_get_admin_nonce(), 'id_placeholder' => __( 'Select a Post', 'gamipress' ), 'id_multiple_placeholder' => __( 'Select Post(s)', 'gamipress' ), 'user_placeholder' => __( 'Select an User', 'gamipress' ), 'post_type_placeholder' => __( 'Default: All', 'gamipress' ), 'rank_placeholder' => __( 'Select a Rank', 'gamipress' ), ) ); } /** * Render shortcode modal insert button * * @param string $editor_id * * @since 1.0.0 */ public function render_button( $editor_id ) { // Prevent to render button inside shortcodes editor window if( $this->rendering_shortcodes ) { return; } $this->button_rendered = true; echo '' . ' ' . 'GamiPress' . ''; } /** * Render shortcode modal content * * @since 1.0.0 */ public function render_modal() { // Return early if button hasn't been rendered if( ! $this->button_rendered ) { return; } $this->rendering_shortcodes = true; ?> rendering_shortcodes = false; } /** * Generate the shortcode selector options * * @since 1.0.0 * @updated 1.7.6 Added support for shortcode groups * * @return string */ private function get_shortcode_selector() { // Setup vars $output = ''; $current_group = ''; $groups = gamipress_get_shortcodes_groups(); $shortcodes = array(); // Make a first loop to reorder all shortcodes by group foreach( $this->shortcodes as $shortcode ) { $group = $shortcode->group; // If group is not registered fallback to others if( ! isset( $groups[$group] ) ) $group = 'others'; // Initialize shortcode group array if( ! isset( $shortcodes[$group] ) ) $shortcodes[$group] = array(); // Add the shortcode to the group $shortcodes[$group][] = $shortcode; } if( isset( $shortcodes['others'] ) ) { // Move others to the end $others = $shortcodes['others']; unset( $shortcodes['others'] ); $shortcodes['others'] = $others; } foreach( $shortcodes as $group => $group_shortcodes ) { // Skip empty groups if( ! is_array( $group_shortcodes ) ) continue; if( $current_group !== $group ) { // Close the previous group if( $current_group !== '' ) $output .= ''; // Render the shortcode group $output .= sprintf( '', $groups[$group] ); // Set the current group for next loop $current_group = $group; } // Render the group shortcodes as options foreach( $group_shortcodes as $shortcode ) $output .= sprintf( '', $shortcode->slug, $shortcode->name ); } return $output; } /** * Render all shortcodes sections * * @since 1.0.0 * * @return string */ private function get_shortcode_sections() { foreach( $this->shortcodes as $shortcode ) { $this->get_shortcode_section( $shortcode ); } } /** * Render a shortcode section * * @since 1.0.0 * * @param GamiPress_Shortcode $shortcode */ private function get_shortcode_section( $shortcode ) { ?>

[slug; ?>] - description; ?>

show_form(); ?>