Initial code drop
This commit is contained in:
184
admin/admin.php
Normal file
184
admin/admin.php
Normal file
@@ -0,0 +1,184 @@
|
||||
<?php
|
||||
add_action('admin_menu', 'mvp_admin_actions');
|
||||
|
||||
add_filter('plugin_action_links_mvp/mvp.php', '_plugin_action_links');
|
||||
|
||||
function mvp_admin_actions() {
|
||||
add_options_page('MVP', 'Superdesk Publisher', 'manage_options', __FILE__, 'mvp_admin');
|
||||
}
|
||||
|
||||
function _plugin_action_links($links) {
|
||||
$links[] = '<a href="' . esc_url(get_page_url()) . '">Settings</a>';
|
||||
return $links;
|
||||
}
|
||||
|
||||
function get_page_url() {
|
||||
|
||||
$args = array('page' => 'mvp/admin/admin');
|
||||
|
||||
$url = add_query_arg($args, admin_url('options-general.php'));
|
||||
|
||||
return $url;
|
||||
}
|
||||
|
||||
function mvp_admin() {
|
||||
if (isset($_POST['url'])) {
|
||||
$settings = array(
|
||||
'url' => $_POST['url'],
|
||||
'username' => $_POST['username'],
|
||||
'password' => $_POST['password'],
|
||||
'client_id' => $_POST['client_id'],
|
||||
'status' => $_POST['status'],
|
||||
'author' => $_POST['author'],
|
||||
'category' => $_POST['category'],
|
||||
);
|
||||
update_option('mvp_settings', $settings);
|
||||
} else if (get_option('mvp_settings')) {
|
||||
$settings = get_option('mvp_settings');
|
||||
} else {
|
||||
$settings = array(
|
||||
'url' => '',
|
||||
'client_id' => '',
|
||||
'username' => '',
|
||||
'password' => '',
|
||||
'status' => 'publish',
|
||||
'author' => '',
|
||||
'category' => '',
|
||||
);
|
||||
}
|
||||
$statuses = array(
|
||||
'publish' => 'Published',
|
||||
'draft' => 'Draft'
|
||||
);
|
||||
|
||||
$authors = array();
|
||||
|
||||
$categories = array();
|
||||
|
||||
$args = array(
|
||||
'hide_empty' => 0,
|
||||
);
|
||||
$all_categories = get_categories($args);
|
||||
|
||||
foreach ($all_categories as $category) {
|
||||
$categories[$category->cat_ID] = $category->cat_name;
|
||||
}
|
||||
|
||||
$all_users = get_users();
|
||||
foreach ($all_users as $user) {
|
||||
$authors[$user->ID] = $user->data->display_name;
|
||||
}
|
||||
?>
|
||||
<div class="wrap">
|
||||
<h2>Superdesk Publisher</h2>
|
||||
<form action="" method="POST">
|
||||
<table class="form-table">
|
||||
<tbody>
|
||||
<tr>
|
||||
<th>Autoload</th>
|
||||
<td><?php echo get_site_url(); ?>/wp-content/plugins/mvp/autoload.php</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<th scope="row">
|
||||
<label for="url">SD (content API) URL</label>
|
||||
</th>
|
||||
<td>
|
||||
<input type="text" name="url" id="url" class="regular-text code" value="<?php echo($settings['url']); ?>">
|
||||
</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<th scope="row">
|
||||
<label for="client_id">Client ID</label>
|
||||
</th>
|
||||
<td>
|
||||
<input type="text" name="client_id" id="client_id" class="regular-text" value="<?php echo($settings['client_id']); ?>">
|
||||
</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<th scope="row">
|
||||
<label for="username">Username</label>
|
||||
</th>
|
||||
<td>
|
||||
<input type="text" name="username" id="username" class="regular-text" value="<?php echo($settings['username']); ?>">
|
||||
</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<th scope="row">
|
||||
<label for="password">Password</label>
|
||||
</th>
|
||||
<td>
|
||||
<input type="password" name="password" id="password" class="regular-text" value="<?php echo($settings['password']); ?>">
|
||||
</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<th scope="row">
|
||||
Status of the ingested articles/posts
|
||||
</th>
|
||||
<td>
|
||||
<fieldset>
|
||||
<?php
|
||||
foreach ($statuses as $key => $value) {
|
||||
?>
|
||||
<label for="status-<?php echo($key); ?>">
|
||||
<input type="radio" name="status" id="status-<?php echo($key); ?>" value="<?php echo($key); ?>"<?php
|
||||
if ($key == $settings['status']) {
|
||||
echo(' checked');
|
||||
}
|
||||
?>> <?php echo($value); ?>
|
||||
</label>
|
||||
<br>
|
||||
<?php
|
||||
}
|
||||
?>
|
||||
</fieldset>
|
||||
</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<th scope="row">
|
||||
<label for="author">Author</label>
|
||||
</th>
|
||||
<td>
|
||||
<select name="author" id="author">
|
||||
<?php
|
||||
foreach ($authors as $key => $value) {
|
||||
?>
|
||||
<option value="<?php echo($key); ?>"<?php
|
||||
if ($key == $settings['author']) {
|
||||
echo(' selected');
|
||||
}
|
||||
?>><?php echo($value); ?></option>
|
||||
<?php
|
||||
}
|
||||
?>
|
||||
</select>
|
||||
</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<th scope="row">
|
||||
<label for="category">Category</label>
|
||||
</th>
|
||||
<td>
|
||||
<select name="category[]" id="category" multiple>
|
||||
<?php
|
||||
foreach ($categories as $key => $value) {
|
||||
?>
|
||||
<option value="<?php echo($key); ?>"<?php
|
||||
if (in_array($key, $settings['category'])) {
|
||||
echo(' selected');
|
||||
}
|
||||
?>><?php echo($value); ?></option>
|
||||
<?php
|
||||
}
|
||||
?>
|
||||
</select>
|
||||
</td>
|
||||
</tr>
|
||||
</tbody>
|
||||
</table>
|
||||
<p class="submit">
|
||||
<input type="submit" name="submit" id="submit" class="button button-primary" value="Save">
|
||||
</p>
|
||||
</form>
|
||||
</div>
|
||||
<?php
|
||||
}
|
||||
98
autoload.php
Normal file
98
autoload.php
Normal file
@@ -0,0 +1,98 @@
|
||||
<?php
|
||||
|
||||
require_once '../../../wp-load.php';
|
||||
|
||||
$json = file_get_contents('php://input');
|
||||
//$json = file_get_contents('log.txt');
|
||||
|
||||
//file_put_contents('log.txt', $json . "\n\n", FILE_APPEND);
|
||||
$obj = json_decode($json, true);
|
||||
|
||||
if ($obj['type'] == 'composite') {
|
||||
|
||||
$settings = get_option('mvp_settings');
|
||||
|
||||
if ($obj['pubstatus'] == 'usable') {
|
||||
$content = $obj['description_html'] . "<!--more-->" . $obj['body_html'];
|
||||
|
||||
$guid = wp_strip_all_tags($obj['guid']);
|
||||
|
||||
$sync = $wpdb->get_row("SELECT post_id FROM " . $wpdb->prefix . DB_TABLE_SYNC_POST . " WHERE guid = '" . $guid . "'");
|
||||
|
||||
if ($sync) {
|
||||
$edit_post = array(
|
||||
'ID' => $sync->post_id,
|
||||
'post_title' => wp_strip_all_tags($obj['headline']),
|
||||
'post_name' => wp_strip_all_tags($obj['headline']),
|
||||
'post_content' => $content,
|
||||
'post_content_filtered' => $content
|
||||
);
|
||||
|
||||
wp_update_post($edit_post);
|
||||
} else {
|
||||
$postarr = array(
|
||||
'post_title' => wp_strip_all_tags($obj['headline']),
|
||||
'post_name' => wp_strip_all_tags($obj['headline']),
|
||||
'post_content' => $content,
|
||||
'post_content_filtered' => $content,
|
||||
'post_author' => (int) $settings['author'],
|
||||
'post_status' => $settings['status'],
|
||||
'post_category' => $settings['category'],
|
||||
);
|
||||
|
||||
$post_ID = wp_insert_post($postarr, true);
|
||||
|
||||
$table_name = $wpdb->prefix . DB_TABLE_SYNC_POST;
|
||||
|
||||
$wpdb->insert(
|
||||
$table_name, array(
|
||||
'post_id' => $post_ID,
|
||||
'guid' => wp_strip_all_tags($obj['guid']),
|
||||
'time' => current_time('mysql')
|
||||
)
|
||||
);
|
||||
|
||||
|
||||
if ($obj['associations']['featuremedia'] && $obj['associations']['featuremedia']['type'] == 'picture') {
|
||||
/* save featured media */
|
||||
$picture = $obj['associations']['featuremedia'];
|
||||
|
||||
$filenameQ = explode("/", $picture['renditions']['original']['media']);
|
||||
$filename = $filenameQ[1];
|
||||
saveFile($picture['renditions']['original']['href'], wp_upload_dir()['path'] . "/" . $filename);
|
||||
|
||||
$attachment = array(
|
||||
'guid' => wp_upload_dir()['url'] . '/' . basename($filename),
|
||||
'post_mime_type' => $picture['mimetype'],
|
||||
'post_title' => preg_replace('/\.[^.]+$/', '', basename($picture['headline'])),
|
||||
'post_content' => '',
|
||||
'post_status' => 'inherit'
|
||||
);
|
||||
|
||||
$attach_id = wp_insert_attachment($attachment, date("Y") . "/" . date("m") . "/" . $filename, $post_ID);
|
||||
|
||||
require_once( ABSPATH . 'wp-admin/includes/image.php' );
|
||||
|
||||
$attach_data = wp_generate_attachment_metadata($attach_id, wp_upload_dir()['path'] . "/" . $filename);
|
||||
|
||||
wp_update_attachment_metadata($attach_id, $attach_data);
|
||||
set_post_thumbnail($post_ID, $attach_id);
|
||||
}
|
||||
}
|
||||
} elseif ($obj['pubstatus'] == 'canceled') {
|
||||
/* remove article */
|
||||
$guid = wp_strip_all_tags($obj['guid']);
|
||||
|
||||
$sync = $wpdb->get_row("SELECT post_id FROM " . $wpdb->prefix . DB_TABLE_SYNC_POST . " WHERE guid = '" . $guid . "'");
|
||||
|
||||
if ($sync) {
|
||||
|
||||
$edit_post = array(
|
||||
'ID' => $sync->post_id,
|
||||
'post_status' => 'draft'
|
||||
);
|
||||
|
||||
wp_update_post($edit_post);
|
||||
}
|
||||
}
|
||||
}
|
||||
48
front/front.php
Normal file
48
front/front.php
Normal file
@@ -0,0 +1,48 @@
|
||||
<?php
|
||||
|
||||
add_shortcode('mvp', 'mvp_content');
|
||||
|
||||
function mvp_content() {
|
||||
|
||||
$settings = get_option('mvp_settings');
|
||||
|
||||
$ch = curl_init();
|
||||
curl_setopt($ch, CURLOPT_URL, $settings['url'] . "/oauth/token");
|
||||
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
|
||||
curl_setopt($ch, CURLOPT_HEADER, FALSE);
|
||||
|
||||
curl_setopt($ch, CURLOPT_POST, TRUE);
|
||||
|
||||
curl_setopt($ch, CURLOPT_POSTFIELDS, 'client_id=' . $settings['client_id'] . '&grant_type=password&username=' . $settings['username'] . '&password=' . $settings['password']);
|
||||
|
||||
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
|
||||
|
||||
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
|
||||
"Content-Type: application/x-www-form-urlencoded"
|
||||
));
|
||||
|
||||
$response = curl_exec($ch);
|
||||
curl_close($ch);
|
||||
|
||||
$config = json_decode($response);
|
||||
|
||||
$ch = curl_init();
|
||||
curl_setopt($ch, CURLOPT_URL, $settings['url'] . "/packages?start_date=" . date("Y-m-d", strtotime('-1 days')));
|
||||
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
|
||||
|
||||
$headers = array('Authorization: ' . $config->token_type . ' ' . $config->access_token);
|
||||
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
|
||||
|
||||
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
|
||||
|
||||
$response = curl_exec($ch);
|
||||
curl_close($ch);
|
||||
|
||||
if ($response) {
|
||||
require_once MVP_PLUGIN_DIR . '/front/wrapper.php';
|
||||
|
||||
$content = mvp_view($response);
|
||||
}
|
||||
|
||||
return $content;
|
||||
}
|
||||
19
front/wrapper.php
Normal file
19
front/wrapper.php
Normal file
@@ -0,0 +1,19 @@
|
||||
<?php
|
||||
|
||||
function mvp_view($data) {
|
||||
if (!empty($data)) {
|
||||
$tmp = json_decode($data);
|
||||
|
||||
foreach ($tmp->_items as $article) {
|
||||
if ($article->pubstatus == "usable") {
|
||||
?>
|
||||
<div class="article">
|
||||
<h2><?php echo $article->headline ?></h2>
|
||||
<?php echo $article->body_html; ?>
|
||||
</div>
|
||||
<?php
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
73
mvp.php
Normal file
73
mvp.php
Normal file
@@ -0,0 +1,73 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* Plugin Name: Superdesk Publisher
|
||||
* Plugin URI:
|
||||
* Description: Nacitani clanku
|
||||
* Version: 0.1
|
||||
* Author: AdminIT
|
||||
*/
|
||||
if (!function_exists('add_action')) {
|
||||
echo 'Hi there! I\'m just a plugin, not much I can do when called directly.';
|
||||
exit;
|
||||
}
|
||||
|
||||
|
||||
define('MVP_VERSION', '0.1');
|
||||
|
||||
define('MVP_REQUIRED_WP_VERSION', '4.2');
|
||||
|
||||
define('MVP_PLUGIN', __FILE__);
|
||||
|
||||
define('MVP_PLUGIN_DIR', untrailingslashit(dirname(MVP_PLUGIN)));
|
||||
|
||||
define('MVP_PLUGIN_URL', untrailingslashit(plugins_url('', MVP_PLUGIN)));
|
||||
|
||||
define('MVP_DATABASE_VERSION', '2');
|
||||
|
||||
define('DB_TABLE_SYNC_POST', 'sync_posts');
|
||||
|
||||
|
||||
require_once MVP_PLUGIN_DIR . '/settings.php';
|
||||
|
||||
function mvp_database_install() {
|
||||
global $wpdb;
|
||||
$table_name = $wpdb->prefix . DB_TABLE_SYNC_POST;
|
||||
|
||||
$charset_collate = $wpdb->get_charset_collate();
|
||||
|
||||
$sql = "CREATE TABLE $table_name (
|
||||
id bigint(20) NOT NULL AUTO_INCREMENT,
|
||||
post_id bigint(20) NOT NULL,
|
||||
guid text NOT NULL,
|
||||
time datetime DEFAULT '0000-00-00 00:00:00' NOT NULL,
|
||||
PRIMARY KEY (id)
|
||||
) $charset_collate;";
|
||||
|
||||
require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
|
||||
dbDelta($sql);
|
||||
|
||||
add_option('MVP_DATABASE_VERSION', MVP_DATABASE_VERSION);
|
||||
}
|
||||
|
||||
function mvp_database_update() {
|
||||
if (get_site_option('MVP_DATABASE_VERSION') != MVP_DATABASE_VERSION) {
|
||||
mvp_database_install();
|
||||
}
|
||||
}
|
||||
|
||||
function saveFile($from, $to) {
|
||||
$ch = curl_init();
|
||||
curl_setopt($ch, CURLOPT_URL, $from);
|
||||
|
||||
$fp = fopen($to, 'w+');
|
||||
curl_setopt($ch, CURLOPT_FILE, $fp);
|
||||
|
||||
curl_exec($ch);
|
||||
|
||||
curl_close($ch);
|
||||
fclose($fp);
|
||||
}
|
||||
|
||||
register_activation_hook(__FILE__, 'mvp_database_install');
|
||||
add_action('plugins_loaded', 'mvp_database_update');
|
||||
8
settings.php
Normal file
8
settings.php
Normal file
@@ -0,0 +1,8 @@
|
||||
<?php
|
||||
|
||||
if (is_admin()) {
|
||||
require_once MVP_PLUGIN_DIR . '/admin/admin.php';
|
||||
} else {
|
||||
require_once MVP_PLUGIN_DIR . '/front/front.php';
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user