Skip to content

Commit 3b4d82d

Browse files
committed
Sync includes from pro (admin cleanup)
1 parent eac2d30 commit 3b4d82d

7 files changed

Lines changed: 169 additions & 136 deletions

File tree

includes/admin/class-settings.php

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1139,19 +1139,22 @@ public function admin_enqueue_scripts( $hook ) {
11391139
if ( ! isset( $this->settings_api->settings_page ) || $hook !== $this->settings_api->settings_page ) {
11401140
return;
11411141
}
1142+
11421143
wp_enqueue_script( 'better-search-admin-js' );
11431144
wp_enqueue_style( 'better-search-admin-ui-css' );
11441145
wp_enqueue_style( 'wp-spinner' );
11451146
wp_localize_script(
11461147
'better-search-admin-js',
11471148
'bsearch_admin_data',
11481149
array(
1149-
'ajax_url' => admin_url( 'admin-ajax.php' ),
1150-
'security' => wp_create_nonce( 'bsearch-admin' ),
1151-
'confirm_message' => esc_html__( 'Are you sure you want to clear the cache?', 'better-search' ),
1152-
'success_message' => esc_html__( 'Cache cleared successfully!', 'better-search' ),
1153-
'fail_message' => esc_html__( 'Failed to clear cache. Please try again.', 'better-search' ),
1154-
'request_fail_message' => esc_html__( 'Request failed: ', 'better-search' ),
1150+
'security' => wp_create_nonce( 'bsearch-admin' ),
1151+
'strings' => array(
1152+
'confirm_message' => esc_html__( 'Are you sure you want to clear the cache?', 'better-search' ),
1153+
'clearing_text' => esc_html__( 'Clearing...', 'better-search' ),
1154+
'success_message' => esc_html__( 'Cache cleared successfully!', 'better-search' ),
1155+
'fail_message' => esc_html__( 'Failed to clear cache. Please try again.', 'better-search' ),
1156+
'request_fail_message' => esc_html__( 'Request failed: ', 'better-search' ),
1157+
),
11551158
)
11561159
);
11571160
}

includes/admin/js/admin-scripts.js

Lines changed: 5 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
jQuery(document).ready(
22
function ($) {
33
$('button[name="bsearch_cache_clear"]').on('click', function () {
4-
if (confirm(bsearch_admin_data.confirm_message)) {
4+
if (confirm(bsearch_admin_data.strings.confirm_message)) {
55
var $button = $(this);
66
$button.prop('disabled', true).append(' <span class="spinner is-active"></span>');
77
clearCache($button);
@@ -10,40 +10,22 @@ jQuery(document).ready(
1010

1111
// Function to clear the cache.
1212
function clearCache($button) {
13-
$.post(bsearch_admin_data.ajax_url, {
13+
$.post(ajaxurl, {
1414
action: 'bsearch_clear_cache',
1515
security: bsearch_admin_data.security
1616
}, function (response) {
1717
if (response.success) {
1818
alert(response.data.message);
1919
} else {
20-
alert(bsearch_admin_data.fail_message);
20+
alert(bsearch_admin_data.strings.fail_message);
2121
}
2222
}).fail(function (jqXHR, textStatus) {
23-
alert(bsearch_admin_data.request_fail_message + textStatus);
23+
alert(bsearch_admin_data.strings.request_fail_message + textStatus);
2424
}).always(function () {
2525
$button.prop('disabled', false).find('.spinner').remove();
2626
});
2727
}
2828

29-
// Prompt the user when they leave the page without saving the form.
30-
var formmodified = 0;
31-
32-
function confirmFormChange() {
33-
formmodified = 1;
34-
}
35-
36-
function confirmExit() {
37-
if (formmodified == 1) {
38-
return true;
39-
}
40-
}
41-
42-
function formNotModified() {
43-
formmodified = 0;
44-
}
45-
46-
$('form *').change(confirmFormChange);
4729

4830
// Collation fix AJAX handler
4931
$(document).on('click', '.bsearch-run-collation-fix', function (e) {
@@ -57,7 +39,7 @@ jQuery(document).ready(
5739
.text('Running...')
5840
.append(' <span class="spinner is-active" style="float: none; margin: 0 0 0 5px;"></span>');
5941

60-
$.post(bsearch_admin_data.ajax_url, {
42+
$.post(ajaxurl, {
6143
action: 'bsearch_run_collation_fix',
6244
blog_id: $button.data('blog-id'),
6345
collation: $button.data('collation'),
@@ -79,14 +61,6 @@ jQuery(document).ready(
7961
});
8062
});
8163

82-
window.onbeforeunload = confirmExit;
83-
84-
$("input[name='submit']").click(formNotModified);
85-
$("input[id='search-submit']").click(formNotModified);
86-
$("input[id='doaction']").click(formNotModified);
87-
$("input[id='doaction2']").click(formNotModified);
88-
$("input[name='filter_action']").click(formNotModified);
89-
9064
$(
9165
function () {
9266
$("#post-body-content").tabs(
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: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,14 @@ private function init(): void {
158158
// Load all hooks.
159159
new Hook_Loader();
160160

161+
// Initialize pro features.
162+
if ( bsearch_freemius()->is__premium_only() ) {
163+
if ( bsearch_freemius()->can_use_premium_code() ) {
164+
$this->pro = new Pro\Pro();
165+
}
166+
Pro\Pro::free_hooks();
167+
}
168+
161169
// Initialize admin on init action to ensure translations are loaded.
162170
Hook_Registry::add_action( 'init', array( $this, 'init_admin' ) );
163171
}

0 commit comments

Comments
 (0)