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

merge

parents 25e4f2d3 5e8e9d72
This diff is collapsed.
<?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();
}
}
?>
\ No newline at end of file
<?php /**
* Truncates HTML text retaining tags and formatting.
* See http://www.pjgalbraith.com/2011/11/truncating-text-html-with-php/ for related blog post.
*
* Example:
*
* $output = TruncateHTML::truncateChars($your_html, '40', '...');
* $output = TruncateHTML::truncateWords($your_html, '7', '...');
*
* @author pjgalbraith http://www.pjgalbraith.com
*
*/
/*
Copyright (c) 2011 Patrick Galbraith (http://www.pjgalbraith.com).
All rights reserved.
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
class TruncateHTML {
public static function truncateChars($html, $limit, $ellipsis = '...') {
if($limit <= 0 || $limit >= strlen(strip_tags($html)))
return $html;
$dom = new DOMDocument();
$dom->loadHTML($html);
$body = $dom->getElementsByTagName("body")->item(0);
$it = new DOMLettersIterator($body);
foreach($it as $letter) {
if($it->key() >= $limit) {
$currentText = $it->currentTextPosition();
$currentText[0]->nodeValue = substr($currentText[0]->nodeValue, 0, $currentText[1] + 1);
self::removeProceedingNodes($currentText[0], $body);
self::insertEllipsis($currentText[0], $ellipsis);
break;
}
}
return preg_replace('~<(?:!DOCTYPE|/?(?:html|head|body))[^>]*>\s*~i', '', $dom->saveHTML());
}
public static function truncateWords($html, $limit, $ellipsis = '...') {
if($limit <= 0 || $limit >= self::countWords(strip_tags($html)))
return $html;
$dom = new DOMDocument();
$dom->loadHTML($html);
$body = $dom->getElementsByTagName("body")->item(0);
$it = new DOMWordsIterator($body);
foreach($it as $word) {
if($it->key() >= $limit) {
$currentWordPosition = $it->currentWordPosition();
$curNode = $currentWordPosition[0];
$offset = $currentWordPosition[1];
$words = $currentWordPosition[2];
$curNode->nodeValue = substr($curNode->nodeValue, 0, $words[$offset][1] + strlen($words[$offset][0]));
self::removeProceedingNodes($curNode, $body);
self::insertEllipsis($curNode, $ellipsis);
break;
}
}
return preg_replace('~<(?:!DOCTYPE|/?(?:html|head|body))[^>]*>\s*~i', '', $dom->saveHTML());
}
private static function removeProceedingNodes(DOMNode $domNode, DOMNode $topNode) {
$nextNode = $domNode->nextSibling;
if($nextNode !== NULL) {
self::removeProceedingNodes($nextNode, $topNode);
$domNode->parentNode->removeChild($nextNode);
} else {
//scan upwards till we find a sibling
$curNode = $domNode->parentNode;
while($curNode !== $topNode) {
if($curNode->nextSibling !== NULL) {
$curNode = $curNode->nextSibling;
self::removeProceedingNodes($curNode, $topNode);
$curNode->parentNode->removeChild($curNode);
break;
}
$curNode = $curNode->parentNode;
}
}
}
private static function insertEllipsis(DOMNode $domNode, $ellipsis) {
$avoid = array('a', 'strong', 'em', 'h1', 'h2', 'h3', 'h4', 'h5'); //html tags to avoid appending the ellipsis to
if( in_array($domNode->parentNode->nodeName, $avoid) && $domNode->parentNode->parentNode !== NULL) {
// Append as text node to parent instead
$textNode = new DOMText($ellipsis);
if($domNode->parentNode->parentNode->nextSibling)
$domNode->parentNode->parentNode->insertBefore($textNode, $domNode->parentNode->parentNode->nextSibling);
else
$domNode->parentNode->parentNode->appendChild($textNode);
} else {
// Append to current node
$domNode->nodeValue = rtrim($domNode->nodeValue).$ellipsis;
}
}
private static function countWords($text) {
$words = preg_split("/[\n\r\t ]+/", $text, -1, PREG_SPLIT_NO_EMPTY);
return count($words);
}
}
?>
\ No newline at end of file
<?php
/**
* Iterates individual characters (Unicode codepoints) 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 DOMLettersIterator($doc) as $letter) echo $letter;
*
* NB: If you only need characters without their position
* in the document, use DOMNode->textContent instead.
*
* @author porneL https://kornel.ski
* @license Public Domain
*
*/
final class DOMLettersIterator implements Iterator
{
private $start, $current;
private $offset, $key, $letters;
/**
* 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 currentTextPosition()
{
return array($this->current, $this->offset);
}
/**
* 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->letters = $m[0];
}
$this->offset++; $this->key++;
if ($this->offset < count($this->letters)) 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->letters[$this->offset];
return NULL;
}
function valid()
{
return !!$this->current;
}
function rewind()
{
$this->offset = -1; $this->letters = array();
$this->current = $this->start;
$this->next();
}
}
......@@ -11,11 +11,14 @@
* @package Sp
* @subpackage Sp/admin/partials
*/
?>
<div class="wrap">
<?php
//Grab all options
$options = get_option($this->plugin_name);
......@@ -44,6 +47,14 @@
$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["twoway"]))
$options["twoway"] = "off";
if (!isset($options["update"]))
$options["update"] = "on";
if (!isset($options["twoway-front"]))
$options["twoway-front"] = "off";
if (!isset($options["wordlimit"]))
$options["wordlimit"] = "0";
$users = get_users();
......@@ -95,8 +106,8 @@
<p> Default tags : <br> separate each tag by a space, 5 max <br> Will be used if you don't specify tags when publishing. </p>
<input type="text" class="regular-text" id="<?php echo $this->plugin_name; ?>-tags" name="<?php echo $this->plugin_name; ?>[tags]" value="<?php echo htmlspecialchars(($options["tags"] == "" ? "steempress blog" : $options["tags"]), ENT_QUOTES); ?>"/>
<br />
<p> Delay posts : Your posts will get published to steem x minutes after being published on your blog. A value of 0 posts your articles to steem as soon as you publish them.</p>
<input type="number" class="regular-text" id="<?php echo $this->plugin_name; ?>-delay" name="<?php echo $this->plugin_name; ?>[delay]" value="<?php echo htmlspecialchars(($options["delay"] == "" ? "0" : $options["delay"]), ENT_QUOTES); ?>"/>
<p> Delay posts : Your posts will get published to steem x minutes after being published on your blog. A value of 0 posts your articles to steem as soon as you publish them. maximum value is 87600, 2 months. </p>
<input type="number" max="87600" class="regular-text" id="<?php echo $this->plugin_name; ?>-delay" name="<?php echo $this->plugin_name; ?>[delay]" value="<?php echo htmlspecialchars(($options["delay"] == "" ? "0" : $options["delay"]), ENT_QUOTES); ?>"/>
<br />
<br />
......@@ -104,6 +115,7 @@
<input type="checkbox" id="<?php echo $this->plugin_name; ?>-vote" name="<?php echo $this->plugin_name; ?>[vote]" <?php echo $options['vote'] == "off" ? '' : 'checked="checked"' ?>> Self vote<br>
<input type="checkbox" id="<?php echo $this->plugin_name; ?>-seo" name="<?php echo $this->plugin_name; ?>[seo]" <?php echo $options['seo'] == "off" ? '' : 'checked="checked"' ?>> Add the footer text to the end of the article.<br>
<input type="checkbox" id="<?php echo $this->plugin_name; ?>-featured" name="<?php echo $this->plugin_name; ?>[featured]" <?php echo $options['featured'] == "off" ? '' : 'checked="checked"' ?>> Add featured images on top of the steem post.<br>
<input type="checkbox" id="<?php echo $this->plugin_name; ?>-update" name="<?php echo $this->plugin_name; ?>[update]" <?php echo $options['update'] == "off" ? '' : 'checked="checked"' ?>> Update the steem post when updating on wordpress.<br>
<br/>
......@@ -146,8 +158,24 @@
?>
<br/>
Two way integration (BETA) <br/>
Displays Steem features including, upvotes, pending rewards, comments and Steem log in on the blog interface. <br/>
<?php
echo "<input type='checkbox' id='".$this->plugin_name."-twoway' name='".$this->plugin_name."[twoway]' ".($options['twoway'] == "on" ? "checked='checked'" : "")."> Activate for posts. <br/>";
echo "<input type='checkbox' id='".$this->plugin_name."-twoway-front' name='".$this->plugin_name."[twoway-front]' ".($options['twoway-front'] == "on" ? "checked='checked'" : "")."> Activate for front page (requires two way integration for posts to be active).";
?>
<br />
<p> Word limit : only publish the first x words to the steem blockchain, set to 0 to publish the entire article. </p>
<input type="number" class="regular-text" id="<?php echo $this->plugin_name; ?>-wordlimit" name="<?php echo $this->plugin_name; ?>[wordlimit]" value="<?php echo htmlspecialchars(($options["wordlimit"] == "" ? "0" : $options["wordlimit"]), ENT_QUOTES); ?>"/>
<br />
<?php
submit_button('Save all changes', 'primary','submit', TRUE); ?>
<?php submit_button('Save all changes', 'primary','submit', TRUE); ?>
</form>
<p><?php
......@@ -163,7 +191,7 @@
$data = array("body" => array("author" => $options['username'], "wif" => $options['posting-key'], "vote" => $options['vote'], "reward" => $options['reward'], "version" => $version, "footer" => $options['footer']));
// Post to the api who will publish it on the steem blockchain.
$result = wp_remote_post($this->api_url."/test", $data);
$result = wp_remote_post(steempress_sp_api_url."/test", $data);
if (is_array($result) or ($result instanceof Traversable)) {
echo "Connectivity to the steem server : <b style='color: darkgreen'>Ok</b> <br/>";
$text = $result['body'];
......@@ -181,7 +209,7 @@
echo "Name : ".$users[$i]->data->display_name."<br/>";
echo "Role : ".$users[$i]->roles[0]."<br/>";
$data = array("body" => array("author" => $options['username'.$users[$i]->data->ID], "wif" => $options['posting-key'.$users[$i]->data->ID], "vote" => $options['vote'], "reward" => $options['reward'], "version" => $version, "footer" => $options['footer']));
$result = wp_remote_post($this->api_url."/test", $data);
$result = wp_remote_post(steempress_sp_api_url."/test", $data);
$text = $result['body'];
if ($text == "ok")
echo "Username/posting key : <b style='color: red'> Wrong</b> <br/> Are you sure you used the private posting key and not the public posting key or password ?<br/>";
......
......@@ -167,14 +167,19 @@ class Steempress_sp {
$this->loader->add_action( 'transition_post_status', $plugin_admin, 'Steempress_sp_post', 15, 3 );
$this->loader->add_filter( 'bulk_actions-edit-post', $plugin_admin,'custom_bulk_actions' );
$this->loader->add_filter( 'handle_bulk_actions-edit-post', $plugin_admin,'custom_bulk_action_handler', 10, 3 );
$this->loader->add_action( 'admin_notices', $plugin_admin,'custom_bulk_action_admin_notice' );
$this->loader->add_filter( 'bulk_actions-edit-post', $plugin_admin,'steempress_sp_bulk_update_action' );
$this->loader->add_filter( 'bulk_actions-edit-post', $plugin_admin,'steempress_sp_bulk_publish_action' );
$this->loader->add_filter( 'handle_bulk_actions-edit-post', $plugin_admin,'steempress_sp_bulk_publish_handler', 10, 3 );
$this->loader->add_filter( 'handle_bulk_actions-edit-post', $plugin_admin,'steempress_sp_bulk_update_handler', 10, 3 );
$this->loader->add_action( 'admin_notices', $plugin_admin,'steempress_sp_bulk_publish_notice' );
$this->loader->add_action( 'admin_notices', $plugin_admin,'steempress_sp_bulk_update_notice' );
$this->loader->add_action( 'post_submitbox_misc_actions', $plugin_admin,'createSteemPublishField' );
$this->loader->add_action( 'post_submitbox_misc_actions', $plugin_admin,'create_update_checkbox' );
$this->loader->add_action( 'save_post', $plugin_admin,'saveSteemPublishField',8);
$this->loader->add_action('add_meta_boxes',$plugin_admin, 'steempress_sp_add_custom_box');
$this->loader->add_action('save_post', $plugin_admin, 'steempress_sp_save_post_data');
}
......@@ -193,7 +198,7 @@ class Steempress_sp {
$plugin_public = new Steempress_sp_Public( $this->get_plugin_name(), $this->get_version() );
$this->loader->add_action( 'wp_enqueue_scripts', $plugin_public, 'enqueue_styles' );
$this->loader->add_action( 'wp_enqueue_scripts', $plugin_public, 'enqueue_scripts' );
$this->loader->add_filter('the_content',$plugin_public, 'steempress_sp_comments');
}
/**
......
......@@ -95,8 +95,68 @@ class Steempress_sp_Public {
* class.
*/
wp_enqueue_script( $this->plugin_name, plugin_dir_url( __FILE__ ) . 'js/steempress_sp-public.js', array( 'jquery' ), $this->version, false );
wp_enqueue_script( $this->plugin_name."iframeResizer", plugin_dir_url( __FILE__ ) . 'js/iframeResizer.min.js');
wp_enqueue_script( $this->plugin_name."public_js", plugin_dir_url( __FILE__ ) . 'js/steempress_sp-public.js', array( 'jquery' ), $this->version, false );
}
public function steempress_sp_comments($content)
{
$options = get_option($this->plugin_name);
if (!isset($options["twoway"]))
$options["twoway"] = "off";
if (!isset($options["twoway-front"]))
$options["twoway-front"] = "off";
if ($options['twoway'] == "on") {
$id = get_the_ID();
$post = get_post($id);
$permlink = get_post_meta($id, "steempress_sp_permlink", true);
$author_id = $post->post_author;
if (!isset($options["username"]))
$options["username"] = "";
$author = $options["username"];
if (isset($options['username' . $author_id]) && $options['username' . $author_id] != "") {
$author = $options['username' . $author_id];
}
$meta_author = get_post_meta($post->ID, 'steempress_sp_author', true);
if ($meta_author != $author && $meta_author != "")
$author = $meta_author;
$steempress = "";
if ($permlink != "" && $author != "") {
// If it's the front page, we display a smaller iframe.
$steempress = "<div id='steempress_sp_comment_feed'>";
if (is_front_page())
$steempress .= "<iframe name='steempress_sp_embed' onload=\"iFrameResize({maxHeight:800 , heightCalculationMethod:'min'})\" src=\"".steempress_sp_twoway_api_url."/?author=".$author."&permlink=".$permlink."&display_comment=false\" style=\"border: 0; width: 100%;margin-bottom: 0px !important;\"></iframe>";
else
$steempress .= "<iframe name='steempress_sp_embed' onload=\"iFrameResize({maxHeight:800, scrolling:true, heightCalculationMethod:'min'})\" src=\"".steempress_sp_twoway_api_url."/?author=".$author."&permlink=".$permlink."&display_comment=true\" style=\"border: 0; width: 100%; margin-bottom: 0px !important;\"></iframe>";
$steempress .= "</div>";
}
if ($options["twoway-front"] === "off" && is_front_page())
return $content;
return $content . $steempress;
} else
return $content;
}
}
/**
* All of the CSS for your public-facing functionality should be
* included in this file.
*/
\ No newline at end of file
/*! iFrame Resizer (iframeSizer.min.js ) - v3.6.2 - 2018-09-16
* Desc: Force cross domain iframes to size to content.
* Requires: iframeResizer.contentWindow.min.js to be loaded into the target frame.
* Copyright: (c) 2018 David J. Bradshaw - [email protected]
* License: MIT
*/
!function(a){"use strict";function b(a,b,c){"addEventListener"in window?a.addEventListener(b,c,!1):"attachEvent"in window&&a.attachEvent("on"+b,c)}function c(a,b,c){"removeEventListener"in window?a.removeEventListener(b,c,!1):"detachEvent"in window&&a.detachEvent("on"+b,c)}function d(){var a,b=["moz","webkit","o","ms"];for(a=0;a<b.length&&!P;a+=1)P=window[b[a]+"RequestAnimationFrame"];P||h("setup","RequestAnimationFrame not supported")}function e(a){var b="Host page: "+a;return window.top!==window.self&&(b=window.parentIFrame&&window.parentIFrame.getId?window.parentIFrame.getId()+": "+a:"Nested host page: "+a),b}function f(a){return M+"["+e(a)+"]"}function g(a){return R[a]?R[a].log:I}function h(a,b){k("log",a,b,g(a))}function i(a,b){k("info",a,b,g(a))}function j(a,b){k("warn",a,b,!0)}function k(a,b,c,d){!0===d&&"object"==typeof window.console&&console[a](f(b),c)}function l(a){function d(){function a(){t(U),q(V),I("resizedCallback",U)}f("Height"),f("Width"),u(a,U,"init")}function e(){var a=S.substr(N).split(":");return{iframe:R[a[0]]&&R[a[0]].iframe,id:a[0],height:a[1],width:a[2],type:a[3]}}function f(a){var b=Number(R[V]["max"+a]),c=Number(R[V]["min"+a]),d=a.toLowerCase(),e=Number(U[d]);h(V,"Checking "+d+" is in range "+c+"-"+b),c>e&&(e=c,h(V,"Set "+d+" to min value")),e>b&&(e=b,h(V,"Set "+d+" to max value")),U[d]=""+e}function g(){function b(){function a(){var a=0,b=!1;for(h(V,"Checking connection is from allowed list of origins: "+d);a<d.length;a++)if(d[a]===c){b=!0;break}return b}function b(){var a=R[V]&&R[V].remoteHost;return h(V,"Checking connection is from: "+a),c===a}return d.constructor===Array?a():b()}var c=a.origin,d=R[V]&&R[V].checkOrigin;if(d&&""+c!="null"&&!b())throw new Error("Unexpected message received from: "+c+" for "+U.iframe.id+". Message was: "+a.data+". This error can be disabled by setting the checkOrigin: false option or by providing of array of trusted domains.");return!0}function k(){return M===(""+S).substr(0,N)&&S.substr(N).split(":")[0]in R}function l(){var a=U.type in{"true":1,"</