Skip to content

Commit f5e4ece

Browse files
committed
Sync includes from pro (admin updates)
1 parent 8e31bcd commit f5e4ece

File tree

7 files changed

+176
-128
lines changed

7 files changed

+176
-128
lines changed

includes/admin/class-settings.php

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1386,30 +1386,32 @@ public static function get_admin_footer_text() {
13861386
*/
13871387
public function admin_enqueue_scripts( $hook ) {
13881388

1389-
if ( empty( $this->settings_api->settings_page ) || $hook !== $this->settings_api->settings_page ) {
1389+
if ( ! isset( $this->settings_api->settings_page ) || $this->settings_api->settings_page !== $hook ) {
13901390
return;
13911391
}
1392+
1393+
wp_enqueue_script( 'top-ten-admin-js' );
1394+
wp_enqueue_style( 'top-ten-admin-css' );
1395+
wp_enqueue_style( 'wp-spinner' );
13921396
wp_localize_script(
1393-
'wz-admin-js',
1397+
'top-ten-admin-js',
13941398
'tptn_admin',
13951399
array(
13961400
'thumb_default' => Display::get_default_thumbnail(),
13971401
)
13981402
);
1399-
wp_enqueue_script( 'top-ten-admin-js' );
1400-
wp_enqueue_style( 'top-ten-admin-css' );
1401-
wp_enqueue_style( 'wp-spinner' );
14021403
wp_localize_script(
14031404
'top-ten-admin-js',
14041405
'top_ten_admin_data',
14051406
array(
1406-
'ajax_url' => admin_url( 'admin-ajax.php' ),
1407-
'security' => wp_create_nonce( 'tptn-admin' ),
1408-
'confirm_message' => esc_html__( 'Are you sure you want to clear the cache?', 'top-10' ),
1409-
'clearing_text' => esc_html__( 'Clearing...', 'top-10' ),
1410-
'success_message' => esc_html__( 'Cache cleared successfully!', 'top-10' ),
1411-
'fail_message' => esc_html__( 'Failed to clear cache. Please try again.', 'top-10' ),
1412-
'request_fail_message' => esc_html__( 'Request failed: ', 'top-10' ),
1407+
'security' => wp_create_nonce( 'tptn-admin' ),
1408+
'strings' => array(
1409+
'confirm_message' => esc_html__( 'Are you sure you want to clear the cache?', 'top-10' ),
1410+
'clearing_text' => esc_html__( 'Clearing...', 'top-10' ),
1411+
'success_message' => esc_html__( 'Cache cleared successfully!', 'top-10' ),
1412+
'fail_message' => esc_html__( 'Failed to clear cache. Please try again.', 'top-10' ),
1413+
'request_fail_message' => esc_html__( 'Request failed: ', 'top-10' ),
1414+
),
14131415
)
14141416
);
14151417
}

includes/admin/js/admin-scripts.js

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,28 @@
11
jQuery(document).ready(function ($) {
22

33
$('button[name="tptn_cache_clear"]').on('click', function () {
4-
if (confirm(top_ten_admin_data.confirm_message)) {
4+
if (confirm(top_ten_admin_data.strings.confirm_message)) {
55
var $button = $(this);
66
var originalText = $button.text();
7-
var clearingText = top_ten_admin_data.clearing_text ? top_ten_admin_data.clearing_text : 'Clearing...';
7+
var clearingText = top_ten_admin_data.strings.clearing_text ? top_ten_admin_data.strings.clearing_text : 'Clearing...';
88
$button.prop('disabled', true).text(clearingText).append(' <span class="spinner is-active"></span>');
99
clearCache($button, originalText);
1010
}
1111
});
1212

1313
// Function to clear the cache.
1414
function clearCache($button, originalText) {
15-
$.post(top_ten_admin_data.ajax_url, {
15+
$.post(ajaxurl, {
1616
action: 'tptn_clear_cache',
1717
security: top_ten_admin_data.security
1818
}, function (response) {
1919
if (response.success) {
2020
alert(response.data.message);
2121
} else {
22-
alert(top_ten_admin_data.fail_message);
22+
alert(top_ten_admin_data.strings.fail_message);
2323
}
2424
}).fail(function (jqXHR, textStatus) {
25-
alert(top_ten_admin_data.request_fail_message + textStatus);
25+
alert(top_ten_admin_data.strings.request_fail_message + textStatus);
2626
}).always(function () {
2727
if ($button && $button.length) {
2828
$button.prop('disabled', false).text(originalText).find('.spinner').remove();
@@ -71,7 +71,7 @@ jQuery(document).ready(function ($) {
7171
var originalText = $button.text();
7272
$button.prop('disabled', true).text('Generating...');
7373

74-
$.post(top_ten_admin_data.ajax_url, {
74+
$.post(ajaxurl, {
7575
action: 'tptn_generate_fast_config',
7676
security: $button.data('nonce')
7777
}, function (response) {
@@ -83,7 +83,7 @@ jQuery(document).ready(function ($) {
8383
}
8484
}).fail(function (jqXHR, textStatus) {
8585
if (window.console && window.console.error) {
86-
window.console.error(top_ten_admin_data.request_fail_message + textStatus);
86+
window.console.error(top_ten_admin_data.strings.request_fail_message + textStatus);
8787
}
8888
}).always(function () {
8989
$button.prop('disabled', false).text(originalText);
@@ -96,7 +96,7 @@ jQuery(document).ready(function ($) {
9696
var originalText = $button.text();
9797
$button.prop('disabled', true).text('Deleting...');
9898

99-
$.post(top_ten_admin_data.ajax_url, {
99+
$.post(ajaxurl, {
100100
action: 'tptn_delete_fast_config',
101101
security: $button.data('nonce')
102102
}, function (response) {
@@ -108,7 +108,7 @@ jQuery(document).ready(function ($) {
108108
}
109109
}).fail(function (jqXHR, textStatus) {
110110
if (window.console && window.console.error) {
111-
window.console.error(top_ten_admin_data.request_fail_message + textStatus);
111+
window.console.error(top_ten_admin_data.strings.request_fail_message + textStatus);
112112
}
113113
}).always(function () {
114114
$button.text(originalText);
Lines changed: 119 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -1,115 +1,155 @@
1+
/**
2+
* CodeMirror Media Insertion functionality.
3+
*
4+
* Handles media library integration for CodeMirror editor instances.
5+
*
6+
* @since 1.0.0
7+
*/
18
jQuery(document).ready(function ($) {
2-
function insertString(editor, str) {
39

10+
/**
11+
* Insert string into CodeMirror editor.
12+
*
13+
* @since 1.0.0
14+
*
15+
* @param {Object} editor CodeMirror instance.
16+
* @param {string} str String to insert.
17+
*/
18+
function insertString(editor, str) {
419
var selection = editor.getSelection();
520

621
if (selection.length > 0) {
722
editor.replaceSelection(str);
823
} else {
9-
1024
var doc = editor.getDoc();
1125
var cursor = doc.getCursor();
12-
1326
var pos = {
1427
line: cursor.line,
1528
ch: cursor.ch
16-
}
29+
};
1730

1831
doc.replaceRange(str, pos);
19-
2032
}
21-
2233
}
2334

24-
// Media selector.
25-
$('.insert-codemirror-media').on('click', function (event) {
26-
event.preventDefault();
35+
/**
36+
* Generate attachment HTML via AJAX.
37+
*
38+
* @since 1.0.0
39+
*
40+
* @param {Object} props Display properties.
41+
* @param {Object} attachment Attachment object.
42+
* @return {Promise} jQuery AJAX promise.
43+
*/
44+
function attachmentHtml(props, attachment) {
45+
var caption = attachment.caption;
46+
var options;
47+
var html;
48+
49+
// Clear caption if disabled globally.
50+
if (!wp.media.view.settings.captions) {
51+
delete attachment.caption;
52+
}
2753

28-
var self = $(this);
29-
var editor = $('#wp-content-editor-container .CodeMirror')[0].CodeMirror;
54+
props = wp.media.string.props(props, attachment);
3055

31-
function attachmentHtml(props, attachment) {
32-
var caption = attachment.caption,
33-
options, html;
56+
options = {
57+
id: attachment.id,
58+
post_content: attachment.description,
59+
post_excerpt: caption
60+
};
3461

35-
// If captions are disabled, clear the caption.
36-
if (!wp.media.view.settings.captions) {
37-
delete attachment.caption;
38-
}
62+
if (props.linkUrl) {
63+
options.url = props.linkUrl;
64+
}
3965

40-
props = wp.media.string.props(props, attachment);
66+
if ('image' === attachment.type) {
67+
html = wp.media.string.image(props);
4168

42-
options = {
43-
id: attachment.id,
44-
post_content: attachment.description,
45-
post_excerpt: caption
46-
};
47-
48-
if (props.linkUrl) {
49-
options.url = props.linkUrl;
50-
}
69+
_.each({
70+
align: 'align',
71+
size: 'image-size',
72+
alt: 'image_alt'
73+
}, function (option, prop) {
74+
if (props[prop]) {
75+
options[option] = props[prop];
76+
}
77+
});
78+
} else if ('video' === attachment.type) {
79+
html = wp.media.string.video(props, attachment);
80+
} else if ('audio' === attachment.type) {
81+
html = wp.media.string.audio(props, attachment);
82+
} else {
83+
html = wp.media.string.link(props);
84+
options.post_title = props.title;
85+
}
5186

52-
if ('image' === attachment.type) {
53-
html = wp.media.string.image(props);
54-
55-
_.each({
56-
align: 'align',
57-
size: 'image-size',
58-
alt: 'image_alt'
59-
}, function (option, prop) {
60-
if (props[prop]) {
61-
options[option] = props[prop];
62-
}
63-
});
64-
} else if ('video' === attachment.type) {
65-
html = wp.media.string.video(props, attachment);
66-
} else if ('audio' === attachment.type) {
67-
html = wp.media.string.audio(props, attachment);
68-
} else {
69-
html = wp.media.string.link(props);
70-
options.post_title = props.title;
87+
return $.ajax({
88+
type: 'POST',
89+
dataType: 'json',
90+
url: ajaxurl,
91+
data: {
92+
action: 'send-attachment-to-editor',
93+
nonce: wp.media.view.settings.nonce.sendToEditor,
94+
attachment: options,
95+
html: html,
96+
post_id: wp.media.view.settings.post.id
7197
}
98+
});
99+
}
72100

73-
return $.ajax({
74-
type: 'POST',
75-
dataType: 'json',
76-
url: ajaxurl,
77-
data: {
78-
action: 'send-attachment-to-editor',
79-
nonce: wp.media.view.settings.nonce.sendToEditor,
80-
attachment: options,
81-
html: html,
82-
post_id: wp.media.view.settings.post.id
83-
},
84-
success: function (response) {
85-
//mediaHtml = response.data;
86-
}
101+
/**
102+
* Process media selection and insert into editor.
103+
*
104+
* @since 1.0.0
105+
*
106+
* @param {Object} editor CodeMirror instance.
107+
* @param {Object} fileFrame Media frame instance.
108+
*/
109+
function processMediaSelection(editor, fileFrame) {
110+
var selection = fileFrame.state().get('selection');
111+
var promises = [];
112+
113+
selection.each(function (attachment) {
114+
var props = fileFrame.state().display(attachment).toJSON();
115+
var promise = attachmentHtml(props, attachment.toJSON()).done(function (response) {
116+
insertString(editor, response.data);
87117
});
88-
}
89118

90-
// Create the media frame.
91-
var file_frame = wp.media.frames.file_frame = wp.media({
92-
frame: 'post',
93-
state: 'insert',
94-
multiple: true
119+
promises.push(promise);
95120
});
96121

97-
file_frame.on('insert', function () {
98-
var selection = file_frame.state().get('selection');
122+
$.when.apply($, promises).always(function () {
123+
fileFrame.close();
124+
});
125+
}
99126

100-
selection.map(function (attachment) {
127+
/**
128+
* Handle media insertion button clicks.
129+
*
130+
* @since 1.0.0
131+
*/
132+
$('.insert-codemirror-media').on('click', function (event) {
133+
event.preventDefault();
134+
event.stopImmediatePropagation();
135+
event.stopPropagation();
136+
$(this).removeClass('add_media');
101137

102-
var props = file_frame.state().display(attachment).toJSON();
138+
var editor = $('#wp-content-editor-container .CodeMirror')[0].CodeMirror;
139+
var fileFrame;
103140

104-
$.when(attachmentHtml(props, attachment.toJSON())).done(function (response) {
105-
mediaHtml = response.data;
106-
insertString(editor, mediaHtml);
107-
});
108-
});
141+
fileFrame = wp.media.frames.file_frame = wp.media({
142+
frame: 'post',
143+
state: 'insert',
144+
multiple: false
145+
});
109146

147+
// Remove old handlers and add insert handler only.
148+
fileFrame.off('insert').on('insert', function () {
149+
processMediaSelection(editor, fileFrame);
110150
});
111151

112-
// Finally, open the modal
113-
file_frame.open();
152+
fileFrame.open();
114153
});
115-
});
154+
155+
});

includes/admin/settings/js/media-selector.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

includes/class-main.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,12 @@ private function init() {
162162
require_once dirname( __DIR__ ) . '/load-freemius.php';
163163
}
164164

165+
if ( tptn_freemius()->is__premium_only() ) {
166+
if ( tptn_freemius()->can_use_premium_code() ) {
167+
$this->pro = new Pro\Pro();
168+
}
169+
}
170+
165171
if ( is_admin() ) {
166172
$this->admin = new Admin\Admin();
167173
if ( is_multisite() ) {

0 commit comments

Comments
 (0)