PluginProbe
GamiPress – Gamification plugin to reward points, badges & ranks in WordPress, now with AI / 7.9.8
GamiPress – Gamification plugin to reward points, badges & ranks in WordPress, now with AI v7.9.8
8.0.4 8.0.3 8.0.1 8.0.2 8.0.0 7.9.9.7 7.9.9.6 7.9.9.5 7.9.9.4 7.9.9.3 7.9.9.2 7.9.9.1 7.9.9 7.9.8 7.9.7 7.9.6 7.9.5 7.9.4 7.9.3 7.9.2 7.9.1 7.9.0 7.8.9 7.8.8 7.8.7 All 46 releases
gamipress / includes / shortcodes / shortcodes-editor.php

shortcodes-editor.php in GamiPress – Gamification plugin to reward points, badges & ranks in WordPress, now with AI 7.9.8, at includes/shortcodes/shortcodes-editor.php

238 lines 7.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * GamiPress Shortcodes Editor Class
4 *
5 * @package GamiPress\Shortcodes\Editor
6 * @author GamiPress <[email protected]>, Ruben Garcia <[email protected]>
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 <p><?php printf( __( 'See the %s page for more information', 'gamipress' ), '<a target="_blank" href="' . admin_url( 'admin.php?page=gamipress_help_support' ) . '">' . __( 'Help/Support', 'gamipress' ) . '</a>' ); ?></p>
102 <div class="alignleft">
103 <select id="select_shortcode"><?php echo $this->get_shortcode_selector(); ?></select>
104 </div>
105 <div class="alignright">
106 <a id="gamipress_insert" class="button-primary" href="#" style="color:#fff;"><?php esc_attr_e( 'Insert Shortcode', 'gamipress' ); ?></a>
107 <a id="gamipress_cancel" class="button-secondary" href="#"><?php esc_attr_e( 'Cancel', 'gamipress' ); ?></a>
108 </div>
109 <div id="shortcode_options" class="alignleft clear">
110 <?php $this->get_shortcode_sections(); ?>
111 </div>
112 </div>
113 </div>
114
115 <?php
116
117 $this->rendering_shortcodes = false;
118 }
119
120 /**
121 * Generate the shortcode selector options
122 *
123 * @since 1.0.0
124 * @updated 1.7.6 Added support for shortcode groups
125 *
126 * @return string
127 */
128 private function get_shortcode_selector() {
129
130 // Setup vars
131 $output = '';
132 $current_group = '';
133 $groups = gamipress_get_shortcodes_groups();
134
135 $shortcodes = array();
136
137 // Make a first loop to reorder all shortcodes by group
138 foreach( $this->shortcodes as $shortcode ) {
139
140 $group = $shortcode->group;
141
142 // If group is not registered fallback to others
143 if( ! isset( $groups[$group] ) ) $group = 'others';
144
145 // Initialize shortcode group array
146 if( ! isset( $shortcodes[$group] ) ) $shortcodes[$group] = array();
147
148 // Add the shortcode to the group
149 $shortcodes[$group][] = $shortcode;
150 }
151
152 if( isset( $shortcodes['others'] ) ) {
153 // Move others to the end
154 $others = $shortcodes['others'];
155 unset( $shortcodes['others'] );
156 $shortcodes['others'] = $others;
157 }
158
159 foreach( $shortcodes as $group => $group_shortcodes ) {
160
161 // Skip empty groups
162 if( ! is_array( $group_shortcodes ) ) continue;
163
164 if( $current_group !== $group ) {
165
166 // Close the previous group
167 if( $current_group !== '' ) $output .= '</optgroup>';
168
169 // Render the shortcode group
170 $output .= sprintf( '<optgroup label="%1$s">', $groups[$group] );
171
172 // Set the current group for next loop
173 $current_group = $group;
174
175 }
176
177 // Render the group shortcodes as options
178 foreach( $group_shortcodes as $shortcode )
179 $output .= sprintf( '<option value="%1$s">%2$s</option>', $shortcode->slug, $shortcode->name );
180
181 }
182
183 return $output;
184 }
185
186 /**
187 * Render all shortcodes sections
188 *
189 * @since 1.0.0
190 *
191 * @return string
192 */
193 private function get_shortcode_sections() {
194 foreach( $this->shortcodes as $shortcode ) {
195 $this->get_shortcode_section( $shortcode );
196 }
197 }
198
199 /**
200 * Render a shortcode section
201 *
202 * @since 1.0.0
203 *
204 * @param GamiPress_Shortcode $shortcode
205 */
206 private function get_shortcode_section( $shortcode ) {
207 ?>
208 <div class="shortcode-section alignleft" id="<?php echo $shortcode->slug; ?>_wrapper">
209 <p><strong>[<?php echo $shortcode->slug; ?>]</strong> - <?php echo $shortcode->description; ?></p>
210
211 <?php $shortcode->show_form(); ?>
212 </div>
213 <?php
214 }
215 }
216
217 /**
218 * Initialize the shortcodes editor.
219 *
220 * @since 1.0.0
221 */
222 function gamipress_shortcodes_add_editor_button() {
223
224 global $pagenow;
225
226 // Prevent render on customizer and widgets
227 if( $pagenow === 'customize.php' || $pagenow === 'widgets.php' ) {
228 return;
229 }
230
231 if( (bool) gamipress_get_option( 'disable_shortcodes_editor', false ) ) {
232 return;
233 }
234
235 new GamiPress_Shortcodes_Editor();
236 }
237 add_action( 'admin_init', 'gamipress_shortcodes_add_editor_button' );
238