This commit is contained in:
Filip Penkava
2017-04-19 13:41:08 +02:00
parent 17880cb75a
commit 10b66659c7
2 changed files with 562 additions and 393 deletions

View File

@@ -24,7 +24,23 @@ function get_page_url() {
} }
function superdesk_admin() { function superdesk_admin() {
$post_formats = get_post_format_strings();
if (isset($_POST['url'])) { if (isset($_POST['url'])) {
$resultArray = array();
if (isset($_POST['post-formats-value'], $_POST['post-formats-input'])) {
foreach ($_POST['post-formats-value'] as $key => $value) {
if (!isset($_POST['post-formats-input'][$key])) {
continue;
}
if (isset($post_formats[$value])) {
$inputValue = $_POST['post-formats-input'][$key];
if (!isset($resultArray[$inputValue]) && !empty($inputValue))
$resultArray[$inputValue] = $value;
}
}
}
$settings = array( $settings = array(
'url' => $_POST['url'], 'url' => $_POST['url'],
'username' => $_POST['username'], 'username' => $_POST['username'],
@@ -49,6 +65,8 @@ function superdesk_admin() {
'slugline-ignored' => $_POST['slugline-ignored'], 'slugline-ignored' => $_POST['slugline-ignored'],
'priority_threshhold' => $_POST['priority_threshhold'], 'priority_threshhold' => $_POST['priority_threshhold'],
'download-images' => $_POST['download-images'], 'download-images' => $_POST['download-images'],
'post-formats' => $_POST['download-images'],
'post-formats-table' => $resultArray,
); );
update_option('superdesk_settings', $settings); update_option('superdesk_settings', $settings);
} else if (get_option('superdesk_settings')) { } else if (get_option('superdesk_settings')) {
@@ -78,6 +96,8 @@ function superdesk_admin() {
'slugline-ignored' => '', 'slugline-ignored' => '',
'priority_threshhold' => '', 'priority_threshhold' => '',
'download-images' => '', 'download-images' => '',
'post-formats' => '',
'post-formats-table' => array(),
); );
} }
$statuses = array( $statuses = array(
@@ -102,6 +122,15 @@ function superdesk_admin() {
foreach ($all_users as $user) { foreach ($all_users as $user) {
$authors[$user->ID] = $user->data->display_name; $authors[$user->ID] = $user->data->display_name;
} }
function make_table_row($format, $text_value, $formats) {
$new_element = '<tr><td><input type="text" name="post-formats-input[]" onkeyup="debounce(validateInput, 250);" class="regular-text" value="' . esc_html($text_value) . '" /></td><td><select name="post-formats-value[]">';
foreach ($formats as $key => $value) {
$new_element .= '<option ' . ($key === $format ? 'selected="selected" ' : '') . 'value="' . esc_html($key) . '">' . esc_html($value) . '</option>';
}
$new_element .= '</select></td><td><a href="#" onclick="removeThisRow(this); return false;">Delete</a></td></tr>';
return $new_element;
}
?> ?>
<div class="wrap"> <div class="wrap">
<h2>Superdesk Publisher</h2> <h2>Superdesk Publisher</h2>
@@ -490,6 +519,52 @@ function superdesk_admin() {
</fieldset> </fieldset>
</td> </td>
</tr> </tr>
<tr>
<th scope="row">
Match Superdesk Content Profiles with Wordpress Post Formats
</th>
<td>
<fieldset>
<label for="post-formats-off">
<input type="radio" name="post-formats" id="post-formats-off" value="off"<?php
if ($settings['post-formats'] == 'off') {
echo(' checked');
}
?>> off
</label>
<br>
<label for="post-formats-on">
<input type="radio" name="post-formats" id="post-formats-on" value="on"<?php
if ($settings['post-formats'] == 'on') {
echo(' checked');
}
?>> on
</label>
</fieldset>
</td>
</tr>
<tr>
<td colspan="2">
<table>
<thead>
<tr>
<th>Superdesk Content Profile Name</th>
<th>Wordpress Post Format</th>
<th><a href="javascript:rowAddFunction();">Add row</a></th>
</tr>
</thead>
<tbody id="post-format-tbody">
<?php
if (isset($settings['post-formats-table']) and is_array($settings['post-formats-table'])) {
foreach ($settings['post-formats-table'] as $key => $value) {
echo make_table_row($value, $key, $post_formats);
}
}
?>
</tbody>
</table>
</td>
</tr>
</tbody> </tbody>
</table> </table>
<p class="submit"> <p class="submit">
@@ -497,5 +572,87 @@ function superdesk_admin() {
</p> </p>
</form> </form>
</div> </div>
<script type="text/javascript">
var select_options = <?php echo json_encode($post_formats); ?>;
var $ = jQuery;
var rowAddFunction = function () {
var element = $("#post-format-tbody");
var new_element = '<tr>\n\
<td><input type="text" name="post-formats-input[]" class="regular-text" />\n\
</td><td>\n\
<select name="post-formats-value[]">';
$.each(select_options, function (key, value) {
new_element += '<option value="' + key + '">' + value + '</option>';
});
new_element += '</select></td><td><a href="#" onclick="removeThisRow(this); return false;">Delete</a></td></tr>';
element.append(new_element);
};
function debounce(func, wait, immediate) {
console.log(func);
var timeout;
return function () {
var context = this, args = arguments;
var later = function () {
timeout = null;
if (!immediate)
func.apply(context, args);
};
var callNow = immediate && !timeout;
clearTimeout(timeout);
timeout = setTimeout(later, wait);
if (callNow)
func.apply(context, args);
};
}
var removeThisRow = function (el) {
var element = $(el).parent()
.parent();
element.remove();
};
var validateInput = function () {
var el = this;
var element = $(el);
var text_value = element.val();
var found = false;
var input_elements = $("#post-format-tbody input");
$.each(input_elements, function (_, input_element) {
if (el !== input_element) {
if (text_value === $(input_element).val()) {
found = true;
return false;
}
}
});
if (found) {
if (!element.hasClass('spwp-input-error')) {
element.addClass('spwp-input-error');
}
} else {
if (element.hasClass('spwp-input-error')) {
element.removeClass('spwp-input-error');
}
}
$("#submit").prop('disabled', found);
};
$(document).ready(function () {
rowAddFunction();
var validateDebounce = debounce(validateInput, 250);
$("#post-format-tbody input").live('keyup', validateDebounce);
});
</script>
<style type="text/css">
.spwp-input-error{
border-color: red !important;
}
</style>
<?php <?php
} }

View File

@@ -27,7 +27,7 @@ if ($obj['type'] == 'text') {
} */ } */
if (!empty($obj['ednote'])) { if (!empty($obj['ednote'])) {
$content.= "<p>Editors Note: " . wp_strip_all_tags($obj['ednote']) . "</p>"; $content .= "<p>Editors Note: " . wp_strip_all_tags($obj['ednote']) . "</p>";
} }
if (isset($obj['evolvedfrom'])) { if (isset($obj['evolvedfrom'])) {
@@ -139,6 +139,12 @@ if ($obj['type'] == 'text') {
'post_category' => $category 'post_category' => $category
); );
if (isset($settings['post-formats'], $settings['post-formats-table']) and ! empty($obj['profile']) and $settings['post-formats'] == 'on') {
if (isset($settings['post-formats-table'][$obj['profile']])) {
set_post_format($post_ID, $settings['post-formats-table'][$obj['profile']]);
}
}
wp_update_post($edit_post); wp_update_post($edit_post);
$attachmentExist = get_post_thumbnail_id($post_ID); $attachmentExist = get_post_thumbnail_id($post_ID);
@@ -177,6 +183,12 @@ if ($obj['type'] == 'text') {
$post_ID = wp_insert_post($postarr, true); $post_ID = wp_insert_post($postarr, true);
if (isset($settings['post-formats'], $settings['post-formats-table']) and ! empty($obj['profile']) and $settings['post-formats'] == 'on') {
if (isset($settings['post-formats-table'][$obj['profile']])) {
set_post_format($post_ID, $settings['post-formats-table'][$obj['profile']]);
}
}
if ($taxonomyTag && !empty($taxonomyTag)) { if ($taxonomyTag && !empty($taxonomyTag)) {
wp_set_post_tags($post_ID, $taxonomyTag); wp_set_post_tags($post_ID, $taxonomyTag);
} }