Commit cb7cb796 authored by Martin Lees's avatar Martin Lees
Browse files

merge

parents 25e4f2d3 5e8e9d72
......@@ -19,6 +19,11 @@
* @package Steempress_sp
* @subpackage Steempress_sp/admin
*/
require('partials/domlettersiterator.php');
require('partials/DOMWordsIterator.php');
require('partials/TruncateHTML.php');
class Steempress_sp_Admin {
/**
......@@ -39,7 +44,6 @@ class Steempress_sp_Admin {
*/
private $version;
private $api_url;
/**
* Initialize the class and set its properties.
*
......@@ -51,8 +55,6 @@ class Steempress_sp_Admin {
$this->plugin_name = $plugin_name;
$this->version = $version;
//$this->api_url = "https://api.steempress.io";
$this->api_url = "http://localhost:8001";
}
/**
......@@ -149,9 +151,13 @@ class Steempress_sp_Admin {
$valid['seo'] = ((isset($input['seo']) && !empty($input['seo'])) && $input['seo'] == 'on') ? 'on' : "off";
$valid['vote'] = ((isset($input['vote']) && !empty($input['vote'])) && $input['vote'] == 'on') ? 'on' : "off";
$valid['append'] = ((isset($input['append']) && !empty($input['append'])) && $input['append'] == 'on') ? 'on' : "off";
$valid['delay'] = ((isset($input['delay']) && !empty($input['delay']) && is_numeric($input['delay']) && $input['delay'] >= 0)) ? htmlspecialchars($input['delay'], ENT_QUOTES) : "0";
$valid['delay'] = ((isset($input['delay']) && !empty($input['delay']) && is_numeric($input['delay']) && $input['delay'] >= 0 && $input['delay'] <= 87600)) ? htmlspecialchars($input['delay'], ENT_QUOTES) : "0";
$valid['featured'] = ((isset($input['featured']) && !empty($input['featured'])) && $input['featured'] == 'on') ? 'on' : "off";
$valid['footer'] = (isset($input['footer']) && !empty($input['footer'])) ? $input['footer'] : "<br /><center><hr/><em>Posted from my blog with <a href='https://wordpress.org/plugins/steempress/'>SteemPress</a> : [%original_link%] </em><hr/></center>";
$valid['twoway'] = ((isset($input['twoway']) && !empty($input['twoway'])) && $input['twoway'] == 'on') ? 'on' : "off";
$valid['twoway-front'] = ((isset($input['twoway-front']) && !empty($input['twoway-front'])) && $input['twoway-front'] == 'on') ? 'on' : "off";
$valid['update'] = ((isset($input['update']) && !empty($input['update'])) && $input['update'] == 'on') ? 'on' : "off";
$valid['wordlimit'] = ((isset($input['wordlimit']) && !empty($input['wordlimit']) && is_numeric($input['wordlimit']) && $input['wordlimit'] >= 0)) ? htmlspecialchars($input['wordlimit'], ENT_QUOTES) : "0";
$users = get_users();
......@@ -206,6 +212,9 @@ class Steempress_sp_Admin {
$options["featured"] = "on";
if (!isset($options["footer"]))
$options["footer"] = "<br /><center><hr/><em>Posted from my blog with <a href='https://wordpress.org/plugins/steempress/'>SteemPress</a> : [%original_link%] </em><hr/></center>";
if (!isset($options["wordlimit"]))
$options["wordlimit"] = "0";
$post = get_post($id);
......@@ -234,8 +243,6 @@ class Steempress_sp_Admin {
if (sizeof($wp_tags) != 0) {
$tags = array();
foreach ($wp_tags as $tag) {
......@@ -275,6 +282,10 @@ class Steempress_sp_Admin {
$version = ((float)$version)*100;
if ($options['wordlimit'] != "0") {
$limit = intval($options["wordlimit"]);
$content = TruncateHTML::truncateWords($content, $limit, '');
}
$data = array("body" => array(
"title" => $post->post_title,
......@@ -299,16 +310,33 @@ class Steempress_sp_Admin {
// Last minute checks before sending it to the server
if ($test['tags'] != "" && $test['author'] != "" && $test['wif'] != "") {
// Post to the api who will publish it on the steem blockchain.
wp_remote_post($this->api_url, $data);
$result = wp_remote_post(steempress_sp_api_url, $data);
if (!isset($result->errors)) {
update_post_meta($id,'steempress_sp_permlink',$result['body']);
update_post_meta($id,'steempress_sp_author',$username);
}
}
}
public function custom_bulk_actions($bulk_actions) {
public function steempress_sp_bulk_update_action($bulk_actions) {
$options = get_option($this->plugin_name);
if (!isset($options["update"]))
$options["update"] = "on";
if ($options["update"] == "on")
$bulk_actions['update_to_steem'] = __('Update to STEEM', 'update_to_steem');
return $bulk_actions;
}
public function steempress_sp_bulk_publish_action($bulk_actions) {
$bulk_actions['publish_to_steem'] = __( 'Publish to STEEM', 'publish_to_steem');
return $bulk_actions;
}
public function custom_bulk_action_handler( $redirect_to, $doaction, $post_ids ) {
public function steempress_sp_bulk_publish_handler( $redirect_to, $doaction, $post_ids ) {
if ( $doaction !== 'publish_to_steem' ) {
return $redirect_to;
}
......@@ -316,16 +344,59 @@ class Steempress_sp_Admin {
// Perform action for each post.
$this->Steempress_sp_publish($post_ids[$i]);
}
$redirect_to = add_query_arg( 'published_to_steem', count( $post_ids ), $redirect_to );
$redirect_to = add_query_arg('published_to_steem', count( $post_ids ), $redirect_to );
return $redirect_to;
}
public function steempress_sp_bulk_update_handler( $redirect_to, $doaction, $post_ids ) {
if ( $doaction !== 'update_to_steem' ) {
return $redirect_to;
}
$updated = 0;
for ($i = sizeof($post_ids)-1; $i >= 0; $i--) {
// Perform action for each post.
if ($this->steempress_sp_update($post_ids[$i], true) == 1)
$updated++;
}
if ($updated != count($post_ids))
$redirect_to = add_query_arg('updated_to_steem_err', $updated, $redirect_to );
else
$redirect_to = add_query_arg('updated_to_steem', $updated, $redirect_to );
return $redirect_to;
}
public function custom_bulk_action_admin_notice() {
if ( ! empty( $_REQUEST['published_to_steem'] ) ) {
public function steempress_sp_bulk_update_notice() {
if ( !empty( $_REQUEST['updated_to_steem']) ) {
$published_count = intval( $_REQUEST['updated_to_steem'] );
printf( '<div id="message" class="updated fade">' .
_n( 'Added %s post to be updated on STEEM. Check your posting queue on <a href="https://steempress.io">https://steempress.io</a> to track the progress.',
'Added %s posts to be updated on STEEM. Check your posting queue on <a href="https://steempress.io">https://steempress.io</a> to track the progress.',
$published_count,
'updated_to_steem'
) . '</div>', $published_count );
}
if (!empty($_REQUEST['updated_to_steem_err']))
{
$published_count = intval( $_REQUEST['updated_to_steem_err'] );
printf( '<div id="message" class="updated fade">' .
_n( 'Your post was not updated probably because the metadata was not correctly set. Please edit the article you wanted to update on STEEM and edit the metadata. Then resubmit it.',
'Added %s posts to be updated on STEEM. Some were not updated probably because the metadata was not correctly set. Please edit the articles you want to update to STEEM and edit the metadata. Then resubmit them.',
$published_count,
'updated_to_steem'
) . '</div>', $published_count );
}
}
public function steempress_sp_bulk_publish_notice() {
if ( !empty($_REQUEST['published_to_steem'])) {
$published_count = intval( $_REQUEST['published_to_steem'] );
printf( '<div id="message" class="updated fade">' .
_n( 'Added %s post to be published on STEEM. STEEM only allows one article to be published per 5 minutes so it may take a while.',
'Added %s posts to be published on STEEM. STEEM only allows one article to be published per 5 minutes so it may take a while.',
_n( 'Added %s post to be published on STEEM. STEEM only allows one article to be published per 5 minutes so it may take a while. Check your posting queue on <a href="https://steempress.io">https://steempress.io</a> to track the progress.',
'Added %s posts to be published on STEEM. STEEM only allows one article to be published per 5 minutes so it may take a while. check your posting queue on <a href="https://steempress.io">https://steempress.io</a> to track the progress.',
$published_count,
'published_to_steem'
) . '</div>', $published_count );
......@@ -333,20 +404,29 @@ class Steempress_sp_Admin {
}
public function Steempress_sp_post($new_status, $old_status, $post)
{
if ('publish' === $new_status && 'publish' !== $old_status && $post->post_type === 'post') {
if (!isset($_POST['Steempress_sp_steem_publish']) && isset($_POST['Steempress_sp_steem_do_not_publish']) ) {
// New post
if ($new_status == 'publish' && $old_status != 'publish' && $post->post_type == 'post') {
if (!isset($_POST['Steempress_sp_steem_publish']) && isset($_POST['Steempress_sp_steem_do_not_publish']) )
return;
} else {
$value = get_post_meta($post->ID, 'Steempress_sp_steem_publish', true);
if ($value != "0")
$this->Steempress_sp_publish($post->ID);
}
}
// Edited post
} else if ($new_status == 'publish' && $old_status == 'publish' && $post->post_type == 'post') {
if (!isset($_POST['Steempress_sp_steem_update']) && isset($_POST['Steempress_sp_steem_do_not_update']) )
return;
$this->steempress_sp_update($post->ID);
}
return;
}
function createSteemPublishField()
{
......@@ -374,27 +454,267 @@ class Steempress_sp_Admin {
<?php
}
function saveSteemPublishField($post_id)
function create_update_checkbox()
{
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) {
$post_id = get_the_ID();
if (get_post_type($post_id) != 'post') {
return;
}
if (
!isset($_POST['Steempress_sp_custom_nonce']) ||
!wp_verify_nonce($_POST['Steempress_sp_custom_nonce'], 'Steempress_sp_custom_nonce_'.$post_id)
) {
if (get_post_status ($post_id) != 'publish')
return;
}
if (!current_user_can('edit_post', $post_id)) {
$options = get_option($this->plugin_name);
if (!isset($options["update"]))
$options["update"] = "on";
if ($options["update"] != "on")
return;
wp_nonce_field('Steempress_sp_custom_update_nonce_'.$post_id, 'Steempress_sp_custom_update_nonce');
$value = get_post_meta($post_id, 'Steempress_sp_steem_update', true);
if ($value == "0")
$checked = "";
else
$checked = "checked";
?>
<div class="misc-pub-section misc-pub-section-last">
<label><input type="checkbox" value="1" <?php echo $checked; ?> name="Steempress_sp_steem_update" /> <input type="hidden" name="Steempress_sp_steem_do_not_update" value="0" />Update to steem </label>
</div>
<?php
}
function saveSteemPublishField($post_id)
{
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)
return;
if ((!isset($_POST['Steempress_sp_custom_nonce']) || !wp_verify_nonce($_POST['Steempress_sp_custom_nonce'], 'Steempress_sp_custom_nonce_'.$post_id)) && (!isset($_POST['Steempress_sp_custom_update_nonce']) || !wp_verify_nonce($_POST['Steempress_sp_custom_update_nonce'], 'Steempress_sp_custom_update_nonce_'.$post_id)))
return;
if (!current_user_can('edit_post', $post_id))
return;
if (get_post_status ($post_id) != 'publish') {
if (isset($_POST['Steempress_sp_steem_publish'])) {
update_post_meta($post_id, 'Steempress_sp_steem_publish', $_POST['Steempress_sp_steem_publish']);
} else {
update_post_meta($post_id, 'Steempress_sp_steem_publish', '0');
}
} else {
if (isset($_POST['Steempress_sp_steem_update'])) {
update_post_meta($post_id, 'Steempress_sp_steem_update', $_POST['Steempress_sp_steem_update']);
} else {
update_post_meta($post_id, 'Steempress_sp_steem_update', '0');
}
}
}
public function steempress_sp_custom_box_html($post)
{
$author_id = $post->post_author;
$options = get_option($this->plugin_name);
if (!isset($options["username"]))
$options["username"] = "";
$author = $options["username"];
if (isset($options['username' . $author_id]) && $options['username' . $author_id] != "") {
$author = $options['username' . $author_id];
}
$permlink = get_post_meta($post->ID, 'steempress_sp_permlink', true);
$meta_author = get_post_meta($post->ID, 'steempress_sp_author', true);
if ($meta_author != $author && $meta_author != "")
$author = $meta_author;
$body = "
<p>These options are only for advanced users regarding steem integration</p>
<label for=\"steempress_sp_author\">Author : </label><br>
<input type='text' name='steempress_sp_author' value='".$author."'/><br>
<label for=\"steempress_sp_author\">Permlink</label>
<input type='text' name='steempress_sp_permlink' value='".$permlink."'/><br>
";
// Minified js to handle the "test parameters" function
$body .= "<script>function steempress_sp_createCORSRequest(){var e=\"".steempress_sp_twoway_api_url."/test_param\",t=new XMLHttpRequest;return\"withCredentials\"in t?t.open(\"POST\",e,!0):\"undefined\"!=typeof XDomainRequest?(t=new XDomainRequest).open(\"POST\",e):t=null,t}function steempress_sp_test_params(){document.getElementById(\"steempress_sp_status\").innerHTML=\"loading...\";var e=steempress_sp_createCORSRequest(),s=document.getElementsByName(\"steempress_sp_author\")[0].value,n=document.getElementsByName(\"steempress_sp_permlink\")[0].value,r=\"username=\"+s+\"&permlink=\"+n;e.setRequestHeader(\"Content-type\",\"application/x-www-form-urlencoded\"),e&&(e.username=s,e.permlink=n,e.onload=function(){var t=e.responseText;document.getElementById(\"steempress_sp_status\").innerHTML=\"ok\"===t?\"The parameters are correct. this article is linked to this <a href='https://steemit.com/@\"+this.username+\"/\"+this.permlink+\"'>steem post</a>\":\"Error : the permlink or username is incorrect.\"},e.send(r))}</script>";
$body .= "<button type=\"button\" onclick='steempress_sp_test_params()'>Test parameters</button><br/><p id='steempress_sp_status'></p>";
echo $body;
}
public function steempress_sp_add_custom_box()
{
$post_id = get_the_ID();
if (get_post_type($post_id) != 'post') {
return;
}
if (get_post_status ($post_id) != 'publish')
return;
add_meta_box(
'steempress_sp_box_id',
'SteemPress options',
array($this,'steempress_sp_custom_box_html'),
'post',
'side'
);
}
function steempress_sp_save_post_data($post_id)
{
if (array_key_exists('steempress_sp_permlink', $_POST) && array_key_exists('steempress_sp_author', $_POST)) {
update_post_meta($post_id,'steempress_sp_permlink',$_POST['steempress_sp_permlink']);
update_post_meta($post_id,'steempress_sp_author',$_POST['steempress_sp_author']);
}
}
/* Returned codes :
1 : ok
-1 : metadata is incorrect
-2 : update is not activated
-3 : Post is not in the published state
*/
function steempress_sp_update($post_id, $bulk = false)
{
$post = get_post($post_id);
if ($post->post_status == "publish") {
$options = get_option($this->plugin_name);
// Avoid undefined errors
if (!isset($options["username"]))
$options["username"] = "";
if (!isset($options["posting-key"]))
$options["posting-key"] = "";
if (!isset($options["reward"]))
$options["reward"] = "100";
if (!isset($options["tags"]))
$options["tags"] = "";
if (!isset($options["seo"]))
$options["seo"] = "on";
if (!isset($options["vote"]))
$options["vote"] = "on";
if (!isset($options["append"]))
$options["append"] = "off";
if (!isset($options["delay"]))
$options["delay"] = "0";
if (!isset($options["featured"]))
$options["featured"] = "on";
if (!isset($options["footer"]))
$options["footer"] = "<br /><center><hr/><em>Posted from my blog with <a href='https://wordpress.org/plugins/steempress/'>SteemPress</a> : [%original_link%] </em><hr/></center>";
if (!isset($options["update"]))
$options["update"] = "on";
if (!isset($options["wordlimit"]))
$options["wordlimit"] = "0";
if ($options["update"] == "on" || $bulk) {
$author_id = $post->post_author;
$username = $options["username"];
$posting_key = $options["posting-key"];
if (isset($options['username' . $author_id]) && $options['username' . $author_id] != "" && isset($options['posting-key' . $author_id]) && $options['posting-key' . $author_id] != "") {
$username = $options['username' . $author_id];
$posting_key = $options['posting-key' . $author_id];
}
$wp_tags = wp_get_post_tags($post_id);
if (sizeof($wp_tags) != 0) {
$tags = array();
foreach ($wp_tags as $tag) {
$tags[] = str_replace(" ", "", $tag->name);
}
$tags = implode(" ", $tags);
if ($options["append"] == "on")
$tags = $options["tags"] . " " . $tags;
} else
$tags = $options["tags"];
$link = get_permalink($post->ID);
if ($options['seo'] == "on")
$display_backlink = "true";
else
$display_backlink = "false";
$content = $post->post_content;
if ($options["featured"] == "on") {
$thumbnail = wp_get_attachment_url(get_post_thumbnail_id($post_id), 'thumbnail');
if ($thumbnail != "0")
$content = "<center>" . $thumbnail . "</center> <br/>" . $post->post_content;
}
$version = steempress_sp_compte;
$pos = strrpos(steempress_sp_compte, ".");
if ($pos !== false)
$version = substr_replace(steempress_sp_compte, "", $pos, strlen("."));
$version = ((float)$version) * 100;
$permlink = get_post_meta($post_id, "steempress_sp_permlink");
if ($options['wordlimit'] != "0") {
$limit = intval($options["wordlimit"]);
$content = TruncateHTML::truncateWords($content, $limit, '');
}
$data = array("body" => array(
"title" => $post->post_title,
"content" => $content,
"tags" => $tags,
"author" => $username,
"wif" => $posting_key,
"original_link" => $link,
"wordpress_id" => $post_id,
"display_backlink" => $display_backlink,
"version" => $version,
"footer" => $options['footer'],
"permlink" => $permlink[0],
"vote"=> $options["vote"],
"reward" => $options['reward'],
));
// A few local verifications as to not overload the server with useless txs
$test = $data['body'];
if ($test['tags'] != "" && $test['author'] != "" && $test['wif'] != "") {
// Post to the api who will update it on the steem blockchain.
$result = wp_remote_post(steempress_sp_api_url . "/update", $data);
if (!isset($result->errors)) {
$data = $result['body'];
if ($data == "ok")
return 1;
else
return -1;
}
} else
return -1;
} else
return -2;
} else
return -3;
}
}
<?php /**
* Iterates individual words of DOM text and CDATA nodes
* while keeping track of their position in the document.
*
* Example:
*
* $doc = new DOMDocument();
* $doc->load('example.xml');
* foreach(new DOMWordsIterator($doc) as $word) echo $word;
*
* @author pjgalbraith http://www.pjgalbraith.com
* @author porneL http://pornel.net (based on DOMLettersIterator available at http://pornel.net/source/domlettersiterator.php)
* @license Public Domain
*
*/
final class DOMWordsIterator implements Iterator {
private $start, $current;
private $offset, $key, $words;
/**
* expects DOMElement or DOMDocument (see DOMDocument::load and DOMDocument::loadHTML)
*/
function __construct(DOMNode $el)
{
if ($el instanceof DOMDocument) $this->start = $el->documentElement;
else if ($el instanceof DOMElement) $this->start = $el;
else throw new InvalidArgumentException("Invalid arguments, expected DOMElement or DOMDocument");
}
/**
* Returns position in text as DOMText node and character offset.
* (it's NOT a byte offset, you must use mb_substr() or similar to use this offset properly).
* node may be NULL if iterator has finished.
*
* @return array
*/
function currentWordPosition()
{
return array($this->current, $this->offset, $this->words);
}
/**
* Returns DOMElement that is currently being iterated or NULL if iterator has finished.
*
* @return DOMElement
*/
function currentElement()
{
return $this->current ? $this->current->parentNode : NULL;
}
// Implementation of Iterator interface
function key()
{
return $this->key;
}
function next()
{
if (!$this->current) return;
if ($this->current->nodeType == XML_TEXT_NODE || $this->current->nodeType == XML_CDATA_SECTION_NODE)
{
if ($this->offset == -1)
{
// fastest way to get individual Unicode chars and does not require mb_* functions
//preg_match_all('/./us',$this->current->textContent,$m); $this->words = $m[0];
$this->words = preg_split("/[\n\r\t ]+/", $this->current->textContent, -1, PREG_SPLIT_NO_EMPTY|PREG_SPLIT_OFFSET_CAPTURE);
}
$this->offset++;
if ($this->offset < count($this->words)) {
$this->key++;
return;
}
$this->offset = -1;
}
while($this->current->nodeType == XML_ELEMENT_NODE && $this->current->firstChild)
{
$this->current = $this->current->firstChild;
if ($this->current->nodeType == XML_TEXT_NODE || $this->current->nodeType == XML_CDATA_SECTION_NODE) return $this->next();
}
while(!$this->current->nextSibling && $this->current->parentNode)
{
$this->current = $this->current->parentNode;
if ($this->current === $this->start) {$this->current = NULL; return;}
}
$this->current = $this->current->nextSibling;
return $this->next();
}
function current()
{
if ($this->current) return $this->words[$this->offset][0];
return NULL;
}
function valid()
{
return !!$this->current;
}
function rewind()
{
$this->offset = -1; $this->words = array();
$this->current = $this->start;
$this->next();
}
}