| 1 |
<?php |
| 2 |
/** |
| 3 |
* GamiPress Shortcodes Editor Class |
| 4 |
* |
| 5 |
* @package GamiPress\Shortcodes\Editor |
| 6 |
* @author GamiPress <contact@gamipress.com>, Ruben Garcia <rubengcdev@gmail.com> |
| 7 |
* @since 1.0.0 |
| 8 |
*/ |
| 9 |
// Exit if accessed directly |
| 10 |
if( !defined( 'ABSPATH' ) ) exit; |
| 11 |
|
| 12 |
class GamiPress_Shortcodes_Editor { |
| 13 |
|
| 14 |
/** |
| 15 |
* @var array $shortcodes All registered shortcodes |
| 16 |
* @since 1.4.7 |
| 17 |
*/ |
| 18 |
public $shortcodes = array(); |
| 19 |
|
| 20 |
/** |
| 21 |
* @var bool $button_rendered Flag to check if button has been rendered |
| 22 |
* @since 1.4.7 |
| 23 |
*/ |
| 24 |
public $button_rendered = false; |
| 25 |
|
| 26 |
/** |
| 27 |
* @var bool $rendering_shortcodes Flag to check if currently is rendering shortcodes |
| 28 |
* @since 1.4.7.1 |
| 29 |
*/ |
| 30 |
public $rendering_shortcodes = false; |
| 31 |
|
| 32 |
public function __construct() { |
| 33 |
|
| 34 |
$this->shortcodes = gamipress_get_shortcodes(); |
| 35 |
|
| 36 |
add_action( 'admin_enqueue_scripts', array( $this, 'admin_scripts' ), 99 ); |
| 37 |
add_action( 'media_buttons', array( $this, 'render_button'), 20 ); |
| 38 |
add_action( 'admin_footer', array( $this, 'render_modal' ) ); |
| 39 |
|
| 40 |
} |
| 41 |
|
| 42 |
/** |
| 43 |
* Enqueue and localize relevant admin scripts |
| 44 |
* |
| 45 |
* @since 1.0.0 |
| 46 |
*/ |
| 47 |
public function admin_scripts( $hook ) { |
| 48 |
|
| 49 |
$min = defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ? '' : '.min'; |
| 50 |
|
| 51 |
// Enqueue shortcodes editor |
| 52 |
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 ); |
| 53 |
|
| 54 |
wp_localize_script( 'gamipress-shortcodes-editor', 'gamipress_shortcodes_editor', array( |
| 55 |
'nonce' => gamipress_get_admin_nonce(), |
| 56 |
'id_placeholder' => __( 'Select a Post', 'gamipress' ), |
| 57 |
'id_multiple_placeholder' => __( 'Select Post(s)', 'gamipress' ), |
| 58 |
'user_placeholder' => __( 'Select an User', 'gamipress' ), |
| 59 |
'post_type_placeholder' => __( 'Default: All', 'gamipress' ), |
| 60 |
'rank_placeholder' => __( 'Select a Rank', 'gamipress' ), |
| 61 |
) ); |
| 62 |
|
| 63 |
} |
| 64 |
|
| 65 |
/** |
| 66 |
* Render shortcode modal insert button |
| 67 |
* |
| 68 |
* @param string $editor_id |
| 69 |
* |
| 70 |
* @since 1.0.0 |
| 71 |
*/ |
| 72 |
public function render_button( $editor_id ) { |
| 73 |
|
| 74 |
// Prevent to render button inside shortcodes editor window |
| 75 |
if( $this->rendering_shortcodes ) { return; } |
| 76 |
|
| 77 |
$this->button_rendered = true; |
| 78 |
|
| 79 |
echo '<a id="insert_gamipress_shortcodes" href="#TB_inline?width=660&height=800&inlineId=select_gamipress_shortcode" class="thickbox button gamipress_media_link" data-width="800">' |
| 80 |
. '<span class="wp-media-buttons-icon dashicons dashicons-gamipress"></span> ' . 'GamiPress' |
| 81 |
. '</a>'; |
| 82 |
} |
| 83 |
|
| 84 |
/** |
| 85 |
* Render shortcode modal content |
| 86 |
* |
| 87 |
* @since 1.0.0 |
| 88 |
*/ |
| 89 |
public function render_modal() { |
| 90 |
|
| 91 |
// Return early if button hasn't been rendered |
| 92 |
if( ! $this->button_rendered ) { return; } |
| 93 |
|
| 94 |
$this->rendering_shortcodes = true; |
| 95 |
|
| 96 |
?> |
| 97 |
|
| 98 |
<div id="select_gamipress_shortcode" style="display:none;"> |
| 99 |
<div class="wrap"> |
| 100 |
<h3><?php _e( 'GamiPress shortcode', 'gamipress' ); ?></h3> |
| 101 |
<div class="alignleft"> |
| 102 |
<select id="select_shortcode"><?php echo $this->get_shortcode_selector(); ?></select> |
| 103 |
</div> |
| 104 |
<div class="alignright"> |
| 105 |
<a id="gamipress_insert" class="button-primary" href="#" style="color:#fff;"><?php esc_attr_e( 'Insert Shortcode', 'gamipress' ); ?></a> |
| 106 |
<a id="gamipress_cancel" class="button-secondary" href="#"><?php esc_attr_e( 'Cancel', 'gamipress' ); ?></a> |
| 107 |
</div> |
| 108 |
<div id="shortcode_options" class="alignleft clear"> |
| 109 |
<?php $this->get_shortcode_sections(); ?> |
| 110 |
</div> |
| 111 |
</div> |
| 112 |
</div> |
| 113 |
|
| 114 |
<?php |
| 115 |
|
| 116 |
$this->rendering_shortcodes = false; |
| 117 |
} |
| 118 |
|
| 119 |
/** |
| 120 |
* Generate the shortcode selector options |
| 121 |
* |
| 122 |
* @since 1.0.0 |
| 123 |
* @updated 1.7.6 Added support for shortcode groups |
| 124 |
* |
| 125 |
* @return string |
| 126 |
*/ |
| 127 |
private function get_shortcode_selector() { |
| 128 |
|
| 129 |
// Setup vars |
| 130 |
$output = ''; |
| 131 |
$current_group = ''; |
| 132 |
$groups = gamipress_get_shortcodes_groups(); |
| 133 |
|
| 134 |
$shortcodes = array(); |
| 135 |
|
| 136 |
// Make a first loop to reorder all shortcodes by group |
| 137 |
foreach( $this->shortcodes as $shortcode ) { |
| 138 |
|
| 139 |
$group = $shortcode->group; |
| 140 |
|
| 141 |
// If group is not registered fallback to others |
| 142 |
if( ! isset( $groups[$group] ) ) $group = 'others'; |
| 143 |
|
| 144 |
// Initialize shortcode group array |
| 145 |
if( ! isset( $shortcodes[$group] ) ) $shortcodes[$group] = array(); |
| 146 |
|
| 147 |
// Add the shortcode to the group |
| 148 |
$shortcodes[$group][] = $shortcode; |
| 149 |
} |
| 150 |
|
| 151 |
if( isset( $shortcodes['others'] ) ) { |
| 152 |
// Move others to the end |
| 153 |
$others = $shortcodes['others']; |
| 154 |
unset( $shortcodes['others'] ); |
| 155 |
$shortcodes['others'] = $others; |
| 156 |
} |
| 157 |
|
| 158 |
foreach( $shortcodes as $group => $group_shortcodes ) { |
| 159 |
|
| 160 |
// Skip empty groups |
| 161 |
if( ! is_array( $group_shortcodes ) ) continue; |
| 162 |
|
| 163 |
if( $current_group !== $group ) { |
| 164 |
|
| 165 |
// Close the previous group |
| 166 |
if( $current_group !== '' ) $output .= '</optgroup>'; |
| 167 |
|
| 168 |
// Render the shortcode group |
| 169 |
$output .= sprintf( '<optgroup label="%1$s">', $groups[$group] ); |
| 170 |
|
| 171 |
// Set the current group for next loop |
| 172 |
$current_group = $group; |
| 173 |
|
| 174 |
} |
| 175 |
|
| 176 |
// Render the group shortcodes as options |
| 177 |
foreach( $group_shortcodes as $shortcode ) |
| 178 |
$output .= sprintf( '<option value="%1$s">%2$s</option>', $shortcode->slug, $shortcode->name ); |
| 179 |
|
| 180 |
} |
| 181 |
|
| 182 |
return $output; |
| 183 |
} |
| 184 |
|
| 185 |
/** |
| 186 |
* Render all shortcodes sections |
| 187 |
* |
| 188 |
* @since 1.0.0 |
| 189 |
* |
| 190 |
* @return string |
| 191 |
*/ |
| 192 |
private function get_shortcode_sections() { |
| 193 |
foreach( $this->shortcodes as $shortcode ) { |
| 194 |
$this->get_shortcode_section( $shortcode ); |
| 195 |
} |
| 196 |
} |
| 197 |
|
| 198 |
/** |
| 199 |
* Render a shortcode section |
| 200 |
* |
| 201 |
* @since 1.0.0 |
| 202 |
* |
| 203 |
* @param GamiPress_Shortcode $shortcode |
| 204 |
*/ |
| 205 |
private function get_shortcode_section( $shortcode ) { |
| 206 |
?> |
| 207 |
<div class="shortcode-section alignleft" id="<?php echo $shortcode->slug; ?>_wrapper"> |
| 208 |
<p><strong>[<?php echo $shortcode->slug; ?>]</strong> - <?php echo $shortcode->description; ?></p> |
| 209 |
|
| 210 |
<?php $shortcode->show_form(); ?> |
| 211 |
</div> |
| 212 |
<?php |
| 213 |
} |
| 214 |
} |
| 215 |
|
| 216 |
/** |
| 217 |
* Initialize the shortcodes editor. |
| 218 |
* |
| 219 |
* @since 1.0.0 |
| 220 |
*/ |
| 221 |
function gamipress_shortcodes_add_editor_button() { |
| 222 |
|
| 223 |
global $pagenow; |
| 224 |
|
| 225 |
// Prevent render on customizer and widgets |
| 226 |
if( $pagenow === 'customize.php' || $pagenow === 'widgets.php' ) { |
| 227 |
return; |
| 228 |
} |
| 229 |
|
| 230 |
if( (bool) gamipress_get_option( 'disable_shortcodes_editor', false ) ) { |
| 231 |
return; |
| 232 |
} |
| 233 |
|
| 234 |
new GamiPress_Shortcodes_Editor(); |
| 235 |
} |
| 236 |
add_action( 'admin_init', 'gamipress_shortcodes_add_editor_button' ); |
| 237 |
|