Repository navigation
Expand file tree
/
Copy pathclass-papi-property-shortcake.php
More file actions
276 lines (236 loc) · 7.92 KB
/
Copy pathclass-papi-property-shortcake.php
File metadata and controls
276 lines (236 loc) · 7.92 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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
<?php
/**
* Property that integrates Shortcake with Papi.
*/
class Papi_Property_Shortcake extends Papi_Property {
/**
* Output CSS in admin head.
*/
public function css() {
?>
<style type="text/css">
.papi-shortcake button.edit-media > span,
.papi-shortcake button.remove-media > span {
margin-top: -2px;
}
.papi-shortcake-container {
float: left;
margin-top: 10px;
overflow-x: hidden;
width: 100%;
}
.papi-shortcake-container > h3 {
padding: 8px 0;
}
.papi-shortcake-container-inner {
margin-bottom: 20px;
}
.papi-shortcake-container-inner iframe,
.papi-shortcake-container-inner object,
.papi-shortcake-container-inner embed {
border: 1px #eaeaea solid;
max-width: 100%;
width: 100%;
}
</style>
<?php
}
/**
* Format the value of the property before it's returned
* to WordPress admin or the site.
*
* @param mixed $value
* @param string $slug
* @param int $post_id
*
* @return bool
*/
public function format_value( $value, $slug, $post_id ) {
return is_admin() ? $value : do_shortcode( $value );
}
/**
* Get shortcode title.
*
* @param string $shortcode
*
* @return string
*/
protected function get_title( $shortcode ) {
$shortcodes = ShortCode_UI::get_instance()->get_shortcodes();
$shortcode = shortcode_parse_atts( $shortcode );
$shortcode = is_array( $shortcode ) && isset( $shortcode[0] ) ? $shortcode[0] : '';
$shortcode = substr( $shortcode, 1 );
if ( isset( $shortcodes[$shortcode] ) ) {
return $shortcodes[$shortcode]['label'];
}
}
/**
* Render property html.
*/
public function html() {
$value = $this->get_value();
$title = $this->get_title( $value );
?>
<div class="papi-shortcake">
<div id="wp-content-media-buttons" class="wp-media-buttons">
<button type="button" class="button insert-media add_media">
<span class="wp-media-buttons-icon"></span>
<?php esc_html_e( 'Add' ); ?>
</button>
<button type="button" class="button edit-media edit_media insert-media <?php echo $value ? '' : 'papi-hide'; ?>">
<span class="wp-media-buttons-icon">
<i class="mce-i-dashicon dashicons-edit"></i>
</span>
<?php esc_html_e( 'Edit' ); ?>
</button>
<button type="button" class="button remove-media remove_media <?php echo $value ? '' : 'papi-hide'; ?>">
<span class="wp-media-buttons-icon">
<i class="mce-i-dashicon dashicons-no"></i>
</span>
<?php esc_html_e( 'Remove' ); ?>
</button>
</div>
<input type="hidden" name="<?php esc_attr_e( $this->html_name() ); ?>" value="<?php esc_attr_e( $value ); ?>">
<div class="papi-shortcake-container">
<?php if ( $title ): ?>
<h3><?php esc_html_e( $title ); ?></h3>
<?php endif; ?>
<div class="papi-shortcake-container-inner <?php echo $value ? '' : 'papi-hide'; ?>">
<?php echo do_shortcode( $value ); // WPCS: xss ok ?>
</div>
</div>
</div>
<?php
}
/**
* Output JavaScript in admin footer.
*/
public function js() {
?>
<script>
/**
* Get shortcode with shortcake attributes.
*
* @param {string} shortcode
*
* @return {object}
*/
function getShortcode(shortcode) {
shortcode = parseShortcode(shortcode);
for (var i = 0, l = sui.shortcodes.models.length; i < l; i++) {
if (sui.shortcodes.models[i].attributes.shortcode_tag === shortcode.tag) {
shortcode.attributes = sui.shortcodes.models[i].attributes;
return shortcode;
}
}
}
/**
* Parse shortcode.
*
* @param {string} shortcode
*
* @return {object}
*/
function parseShortcode(shortcode) {
var shortcodeTags = _.map( sui.shortcodes.pluck( 'shortcode_tag' ), pregQuote ).join( '|' );
var regexp = wp.shortcode.regexp( shortcodeTags );
regexp.lastIndex = 0;
return wp.shortcode.fromMatch( regexp.exec( shortcode ) );
}
/**
* Escape any special characters in a string to be used as a regular expression.
*
* JavaScript version of PHP's `preg_quote`.
*
* @see http://phpjs.org/functions/preg_quote/
*
* @param {string} str
* @param {string} delimiter
* @return {string}
*/
function pregQuote(str, delimiter) {
return String(str).replace(
new RegExp( '[.\\\\+*?\\[\\^\\]$(){}=!<>|:\\' + ( delimiter || '' ) + '-]', 'g' ),
'\\$&'
);
}
// Insert media element event.
jQuery(document).on('click', '.papi-shortcake button.insert-media', function(e) {
e.preventDefault();
var $this = jQuery(e.currentTarget);
var $target = $this.closest('.papi-shortcake').find('.papi-shortcake-container > div');
var $hidden = $this.closest('.papi-shortcake').find('input[type="hidden"]');
// Store old version of `send_to_editor`.
window.send_to_editor_old = window.send_to_editor;
// Remove media frame menu and modify css.
jQuery('.media-modal-content .media-menu .separator:first').next().trigger('click').closest('.media-frame-menu').remove();
jQuery('.media-modal-content .media-frame-title, .media-frame-content').css('left', '0px');
jQuery('.media-modal-content .media-toolbar-primary.search-form').css('width', '12.7%');
// In Edit mode.
if ($this.hasClass('edit_media')) {
var shortcode = getShortcode($hidden.val());
jQuery('.media-modal-content .add-shortcode-list li[data-shortcode="' + shortcode.attributes.shortcode_tag + '"]').trigger('click');
jQuery('.media-modal-content .edit-shortcode-form-cancel').remove();
jQuery('.media-modal-content .media-frame-title h1').text(shortcode.attributes.label + ' <?php esc_html_e( 'Details' ); ?>');
for (var key in shortcode.attrs.named) {
if (shortcode.attrs.named.hasOwnProperty(key)) {
jQuery('.media-modal-content .edit-shortcode-form [name="' + key + '"]').val(shortcode.attrs.named[key]);
}
}
}
// Media frame close event.
wp.media.frame.on('close', function() {
// Restore old `send_to_editor` if it exists.
if (typeof window.send_to_editor_old !== 'undefined') {
window.send_to_editor = window.send_to_editor_old;
delete window.send_to_editor_old;
}
});
// Override `send_to_editor`.
window.send_to_editor = function(value) {
jQuery.getJSON(papi.ajaxUrl + '?action=get_shortcode&shortcode=' + encodeURIComponent(value), function(res) {
$target.removeClass('papi-hide').html(res.html);
$hidden.val(value);
if (shortcode = getShortcode(value)) {
$target.siblings().remove();
jQuery('<h3>' + shortcode.attributes.label + '</h3>').insertBefore($target);
}
// Show edit and remove buttons.
jQuery('.papi-shortcake button.edit-media').removeClass('papi-hide');
jQuery('.papi-shortcake button.remove-media').removeClass('papi-hide');
});
// Restore old `send_to_editor`.
window.send_to_editor = window.send_to_editor_old;
delete window.send_to_editor_old;
};
});
// Edit media element event.
jQuery(document).on('click', '.papi-shortcake button.remove-media', function(e) {
e.preventDefault();
var $this = jQuery(e.currentTarget);
var $hidden = $this.closest('.papi-shortcake').find('input[type="hidden"]');
});
// Remove media element event.
jQuery(document).on('click', '.papi-shortcake button.remove-media', function(e) {
e.preventDefault();
var $this = jQuery(e.currentTarget);
var $prop = $this.closest('.papi-shortcake');
// Clear container div.
$prop.find('.papi-shortcake-container > h3').remove();
$prop.find('.papi-shortcake-container > div').addClass('papi-hide').html('');
$prop.find('input[type="hidden"]').val('');
// Hide edit and remove buttons
$prop.find('button.edit-media').addClass('papi-hide');
$this.addClass('papi-hide');
});
</script>
<?php
}
/**
* Setup actions.
*/
protected function setup_actions() {
add_action( 'admin_head', [$this, 'css'] );
add_action( 'admin_footer', [$this, 'js'] );
}
}