-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathadmin.js
More file actions
162 lines (130 loc) · 5.54 KB
/
admin.js
File metadata and controls
162 lines (130 loc) · 5.54 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
jQuery(document).ready(function($) {
console.log('WordPress to Threads admin script loaded');
console.log('threads_ajax object:', threads_ajax);
// Handle thread chain options visibility
function toggleThreadChainOptions() {
var isEnabled = $('input[name="threads_enable_thread_chains"]').is(':checked');
var maxChainRow = $('select[name="threads_max_chain_length"]').closest('tr');
var splitMethodRow = $('select[name="threads_split_preference"]').closest('tr');
if (isEnabled) {
maxChainRow.show();
splitMethodRow.show();
} else {
maxChainRow.hide();
splitMethodRow.hide();
}
}
// Initialize visibility on page load
toggleThreadChainOptions();
// Toggle visibility when checkbox changes
$('input[name="threads_enable_thread_chains"]').on('change', function() {
toggleThreadChainOptions();
});
$('.threads-post-btn').on('click', function(e) {
e.preventDefault();
console.log('Post button clicked');
var button = $(this);
var postId = button.data('post-id');
var row = button.closest('tr');
console.log('Post ID:', postId);
console.log('Button element:', button);
if (!postId) {
alert('Error: No post ID found');
return;
}
if (typeof threads_ajax === 'undefined') {
alert('Error: AJAX configuration not loaded');
return;
}
button.prop('disabled', true).text('Posting...');
var ajaxData = {
action: 'threads_manual_post',
post_id: postId,
nonce: threads_ajax.nonce
};
console.log('AJAX data:', ajaxData);
console.log('AJAX URL:', threads_ajax.ajax_url);
$.ajax({
url: threads_ajax.ajax_url,
type: 'POST',
data: ajaxData,
success: function(response) {
console.log('AJAX Success Response:', response);
if (response.success) {
// Reload the page to show updated status
location.reload();
} else {
button.prop('disabled', false).text('Post to Threads');
$('#threads-post-results').prepend(
'<div class="notice notice-error is-dismissible"><p>Error: ' +
response.data + '</p></div>'
);
}
},
error: function(xhr, status, error) {
console.log('AJAX Error:', {xhr: xhr, status: status, error: error});
console.log('Response Text:', xhr.responseText);
button.prop('disabled', false).text('Post to Threads');
$('#threads-post-results').prepend(
'<div class="notice notice-error is-dismissible"><p>Network error: ' + error + ' (Status: ' + status + ')</p></div>'
);
}
});
});
// Re-post button handler
$('.threads-repost-btn').on('click', function(e) {
e.preventDefault();
console.log('Re-post button clicked');
var button = $(this);
var postId = button.data('post-id');
var row = button.closest('tr');
console.log('Re-post Post ID:', postId);
if (!postId) {
alert('Error: No post ID found');
return;
}
if (typeof threads_ajax === 'undefined') {
alert('Error: AJAX configuration not loaded');
return;
}
// Confirm re-posting
if (!confirm('Are you sure you want to re-post this to Threads? This will create a new post even if it was already shared.')) {
return;
}
button.prop('disabled', true).text('Re-posting...');
var ajaxData = {
action: 'threads_repost',
post_id: postId,
nonce: threads_ajax.nonce
};
console.log('Re-post AJAX data:', ajaxData);
$.ajax({
url: threads_ajax.ajax_url,
type: 'POST',
data: ajaxData,
success: function(response) {
console.log('Re-post AJAX Success Response:', response);
if (response.success) {
// Reload the page to show updated status
location.reload();
} else {
button.prop('disabled', false).text('Re-post');
$('#threads-post-results').prepend(
'<div class="notice notice-error is-dismissible"><p>Error: ' +
response.data + '</p></div>'
);
}
},
error: function(xhr, status, error) {
console.log('Re-post AJAX Error:', {xhr: xhr, status: status, error: error});
button.prop('disabled', false).text('Re-post');
$('#threads-post-results').prepend(
'<div class="notice notice-error is-dismissible"><p>Network error: ' + error + ' (Status: ' + status + ')</p></div>'
);
}
});
});
$(document).on('click', '.notice-dismiss', function() {
$(this).closest('.notice').fadeOut();
});
});