class.BadgeOS_Editor_Shortcodes.php
8.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
<?php
/**
* BadgeOS Shortcodes WP Editor Class.
*
* @package BadgeOS
* @subpackage Classes
* @author LearningTimes, LLC
* @license http://www.gnu.org/licenses/agpl.txt GNU AGPL v3.0
* @link https://credly.com
*/
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly.
}
class BadgeOS_Editor_Shortcodes {
public function __construct() {
$this->directory_path = plugin_dir_path( dirname( __FILE__ ) );
$this->directory_url = plugin_dir_url( dirname( __FILE__ ) );
$this->shortcodes = badgeos_get_shortcodes();
add_action( 'media_buttons', array( $this, 'render_button' ), 20 );
add_action( 'admin_enqueue_scripts', array( $this, 'admin_scripts' ), 99 );
add_action( 'admin_footer', array( $this, 'render_modal' ) );
}
/**
* Enqueue and localize relevant admin_scripts.
*
* @since 1.4.0
*/
public function admin_scripts() {
global $pagenow;
$min = defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ? '' : '.min';
wp_enqueue_script( 'badgeos-select2', $this->directory_url . "js/select2/select2$min.js", array( 'jquery' ), '', true );
wp_enqueue_style( 'badgeos-select2-css', $this->directory_url . "js/select2/select2$min.css" );
wp_enqueue_style( 'badgeos-juqery-ui-css', $this->directory_url . "css/jquery-ui$min.css" );
wp_enqueue_style( 'badgeos-juqery-autocomplete-css', $this->directory_url . "css/autocomplete$min.css" );
if ( $pagenow === 'post.php' || $pagenow === 'post-new.php' ) {
wp_enqueue_script( 'badgeos-jquery-ui-js' );
wp_enqueue_script( 'badgeos-shortcodes-embed', $this->directory_url . "js/badgeos-shortcode-embed$min.js", array( 'jquery' ), '', true );
}
wp_localize_script( 'badgeos-shortcodes-embed', 'badgeos_shortcode_embed_messages', $this->get_localized_text() );
}
/**
* Get localized JS text.
*
* @since 1.4.0
*
* @return array Array of translated text
*/
public function get_localized_text() {
$badgeos_settings = ( $exists = badgeos_utilities::get_option( 'badgeos_settings' ) ) ? $exists : array();
$achievement_types = get_posts(
array(
'post_type' => $badgeos_settings['achievement_main_post_type'],
'posts_per_page' => -1,
)
);
$select_options = '';
foreach ( $achievement_types as $type ) {
$select_options .= '<option value="' . $type->post_name . '">' . $type->post_title . '</option>';
}
return array(
'id_placeholder' => esc_html__( 'Select a Post', 'badgeos' ),
'id_multiple_placeholder' => esc_html__( 'Select Post(s)', 'badgeos' ),
'user_placeholder' => esc_html__( 'Select a user', 'badgeos' ),
'post_type_placeholder' => esc_html__( 'Default: All', 'badgeos' ),
'achievements_select_options' => $select_options,
);
}
/**
* Render shortcode modal insert button.
*
* @since 1.4.0
*/
public function render_button() {
$min = defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ? '' : '.min';
echo '<a id="insert_badgeos_shortcodes" href="#TB_inline?width=660&height=800&inlineId=select_badgeos_shortcode" class="thickbox button badgeos_media_link" data-width="800">' . esc_html__( 'Add BadgeOS Shortcode', 'badgeos' ) . '</a>';
}
/**
* Render shortcode modal content.
*
* @since 1.4.0
*/
public function render_modal() { ?>
<div id="select_badgeos_shortcode" style="display:none;">
<div class="wrap">
<h3><?php esc_html_e( 'Insert a BadgeOS shortcode', 'badgeos' ); ?></h3>
<p><?php printf( esc_html__( 'See the %s page for more information', 'badgeos' ), '<a target="_blank" href="' . esc_url( admin_url( 'admin.php?page=badgeos_sub_help_support' ) ) . '">' . esc_html__( 'Help/Support', 'badgeos' ) . '</a>' ); ?></p>
<div class="alignleft">
<select id="select_shortcode">
<?php echo $this->get_shortcode_selector(); ?></select>
</div>
<div class="alignright">
<a id="badgeos_insert" class="button-primary" href="#" style="color:#fff;"><?php esc_attr_e( 'Insert Shortcode', 'badgeos' ); ?></a>
<a id="badgeos_cancel" class="button-secondary" href="#"><?php esc_attr_e( 'Cancel', 'badgeos' ); ?></a>
</div>
<div id="shortcode_options" class="alignleft clear">
<?php echo $this->get_shortcode_sections(); ?>
</div>
</div>
</div>
<?php
}
private function get_shortcode_selector() {
$output = '';
foreach ( $this->shortcodes as $shortcode ) {
$output .= sprintf( '<option value="%1$s">%2$s</option>', esc_attr( $shortcode->slug ), esc_html( $shortcode->name ) );
}
return $output;
}
private function get_shortcode_sections() {
$output = '';
foreach ( $this->shortcodes as $shortcode ) {
$output .= $this->get_shortcode_section( $shortcode );
}
return $output;
}
private function get_shortcode_section( $shortcode = array() ) {
$output = '<div class="shortcode-section alignleft" id="' . esc_attr( $shortcode->slug ) . '_wrapper">';
$output .= sprintf( '<p><strong>%1$s</strong> - %2$s</p>', "[{$shortcode->slug}]", esc_html( $shortcode->description ) );
foreach ( $shortcode->attributes as $slug => $attribute ) {
$attribute['slug'] = $slug;
if ( $slug === 'type' ) {
$attribute['default'] = '';
$attribute['values'] = '';
}
$element_slugs = array( 'type', 'user_id', 'include', 'exclude', 'achievement_id', 'id' );
if ( in_array( $slug, $element_slugs ) ) {
$attribute['type'] = ( $attribute['type'] === 'text' ) ? 'select' : $attribute['type'];
}
$output .= $this->get_input(
array(
'shortcode' => $shortcode,
'attribute' => $attribute,
)
);
}
$output .= '</div>';
return $output;
}
private function get_input( $args = array() ) {
switch ( $args['attribute']['type'] ) {
case 'select':
return $this->get_select_input( $args );
case 'text':
case 'select2':
default:
return $this->get_text_input( $args );
}
}
private function get_text_input( $args = array() ) {
$name_field = $args['attribute']['slug'];
$name_field_type = 'normal';
$hidden_field = '';
if ( isset( $args['attribute']['autocomplete_name'] ) && ! empty( $args['attribute']['autocomplete_name'] ) ) {
$name_field = $args['attribute']['autocomplete_name'];
$name_field_type = 'autocomplete';
$hidden_field = sprintf(
'<input class="%1$s %2$s" id="%1$s" name="%3$s" type="hidden" data-slug="%3$s" data-shortcode="%4$s" value="%5$s" />',
$args['shortcode']->slug . '_' . $name_field,
$args['attribute']['type'],
$name_field,
$args['shortcode']->slug,
$args['attribute']['default']
);
}
return $hidden_field . sprintf(
'<div class="badgeos_input alignleft">
<label for="%1$s">%2$s</label>
<br/>
<input class="%1$s %4$s" id="%1$s" name="%5$s" type="text" data-fieldname="%8$s" data-type="%9$s" data-slug="%5$s" data-shortcode="%6$s" value="%7$s" />
<p class="description">%3$s</p>
</div>
',
$args['shortcode']->slug . '_' . $args['attribute']['slug'],
$args['attribute']['name'],
$args['attribute']['description'],
$args['attribute']['type'],
$args['attribute']['slug'],
$args['shortcode']->slug,
$args['attribute']['default'],
$args['shortcode']->slug . '_' . $name_field,
$name_field_type
);
}
private function get_select_input( $args = array() ) {
return sprintf(
'
<div class="badgeos_input alignleft">
<label for="%1$s">%2$s</label>
<br/>
<select class="%1$s %7$s" id="%1$s" name="%5$s" data-slug="%5$s" data-shortcode="%6$s">%3$s</select>
<p class="description">%4$s</p>
</div>
',
$args['shortcode']->slug . '_' . $args['attribute']['slug'],
$args['attribute']['name'],
$this->get_select_options( $args['attribute'] ),
$args['attribute']['description'],
$args['attribute']['slug'],
$args['shortcode']->slug,
$args['attribute']['type']
);
}
private function get_select_options( $attribute = array() ) {
$options = array();
if ( ! empty( $attribute['values'] ) ) {
foreach ( $attribute['values'] as $value => $label ) {
$options[] = sprintf(
'<option %1$s value="%2$s">%3$s</option>',
selected( $value, $attribute['default'], false ),
$value,
$label
);
}
}
return implode( "\n", $options );
}
}
function badgeos_shortcodes_add_editor_button() {
new BadgeOS_Editor_Shortcodes();
}
add_action( 'admin_init', 'badgeos_shortcodes_add_editor_button' );