all repos — blogroll2email @ 0f945520ea29b3cee4b71503e3833f1220275972

releasing 0.3
Peter Molnar hello@petermolnar.eu
Tue, 12 Jan 2016 16:37:00 +0000
commit

0f945520ea29b3cee4b71503e3833f1220275972

parent

c41d438148452a7ea29fa7648a34ea3fcc96e0de

2 files changed, 105 insertions(+), 67 deletions(-)

jump to
M blogroll2email.phpblogroll2email.php

@@ -27,6 +27,11 @@ along with this program; if not, write to the Free Software

Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */ +if ( !class_exists('Mf2\Parser') ) { + require (__DIR__ . '/vendor/autoload.php'); +} + + if (!class_exists('blogroll2email')): class blogroll2email {

@@ -120,6 +125,8 @@

$currowner = $owner = false; foreach ( $bookmarks as $bookmark ) { + + /* print_r ($bookmark); stdClass Object (

@@ -160,11 +167,6 @@ )

*/ } - if ( empty($bookmark->link_rss)) - $this->parse_mf ( $bookmark, $owner ); - else - $this->parse_rss( $bookmark, $owner ); - $export_yaml[ $owner->user_nicename ][] = array ( 'name' => $bookmark->link_name, 'url' => $bookmark->link_url,

@@ -172,6 +174,46 @@ 'rss' => $bookmark->link_rss,

'description' => $bookmark->link_description, 'lastfetched' => $bookmark->link_updated, ); + + if ( !empty($bookmark->link_rss)) { + $this->do_rss( $bookmark, $owner ); + } + else { + static::debug('Switcing into HTML mode'); + $url = htmlspecialchars_decode($bookmark->link_url); + + static::debug(" fetching {$url}"); + $q = wp_remote_get($url); + + if (is_wp_error($q)) { + static::debug(' something went wrong: ' . $q->get_error_message()); + continue; + } + + if (!is_array($q)) + continue; + + if (!isset($q['headers']) || !is_array($q['headers'])) + continue; + + if (!isset($q['body']) || empty($q['body'])) + continue; + + $ctype = isset($q['headers']['content-type']) ? $q['headers']['content-type'] : 'text/html'; + + if ($ctype == "application/json") { + static::debug(" content is json"); + $content = json_decode($q['body'], true); + } + else { + static::debug(" content is html"); + $content = Mf2\parse($q['body'], $url); + } + + static::debug(" sending it to mf parser"); + $this->parse_mf ( $bookmark, $owner, $content ); + } + } if (function_exists('yaml_emit')) {

@@ -280,7 +322,7 @@ *

* @param object $bookmark * @param object $owner */ - protected function parse_rss ( $bookmark, $owner ) { + protected function do_rss ( $bookmark, $owner ) { // no bookmark, no fun if ( empty ($bookmark) || !is_object ($bookmark))

@@ -400,7 +442,7 @@ * @param object $bookmark

* @param object $owner * */ - protected function parse_mf ( $bookmark, $owner ) { + protected function parse_mf ( $bookmark, $owner, $mf ) { if ( empty ($bookmark) || !is_object ($bookmark)) return false;

@@ -408,78 +450,67 @@

if ( empty ($owner) || !is_object ($owner)) return false; - if ( !class_exists('Mf2') ) { - require __DIR__ . '/vendor/autoload.php'; - //use Mf2; + $last_updated = strtotime( $bookmark->link_updated ); + + $mfitems = $items = array (); + + static::debug(" looping topitems"); + foreach ($mf['items'] as $topitem ) { + + if ( in_array( 'h-feed', $topitem['type']) && !empty($topitem['children'])) { + $mfitems[] = $topitem['children']; + } + elseif ( in_array( 'h-entry', $topitem['type']) ) { + $mfitems[] = $topitem; + } } - $last_updated = strtotime( $bookmark->link_updated ); + static::debug(" looping mfitems"); + foreach ($mfitems as $entry ) { + // double-check h-entries + if ( in_array( 'h-entry', $entry['type']) ) { - $url = htmlspecialchars_decode($bookmark->link_url); + $properties = $entry['properties']; + $title = $bookmark->link_name; - //$hash = md5( $url ); - // check cache file and skip this step if exists ? + if (isset($properties['name'][0]) && !empty($properties['name'][0])) + $title = $properties['name'][0]; - $mf = Mf2\fetch($url, true, $curlInfo); - error_log('MF2 fetching: ' . $url ); + elseif(isset($properties['author'][0]['properties']['name'][0]) && !empty($properties['author'][0]['properties']['name'][0]) ) + $title = $properties['author'][0]['properties']['name'][0]; - // check for rss, because it's older and more mature - // if there is one, register it for the link and skip this run - // in the next run, we'll parse the RSS - if ( isset($mf['alternates']) && !empty($mf['alternates'])) { - foreach ( $mf['alternates'] as $alternate ) { - if ( isset($alternate['type']) && - !empty($alternate['type']) && - $alternate['type'] == 'application/rss+xml' - && isset($altenate['url']) && - !empty($alternate['ulr']) && - filter_var($alternate['ulr'], FILTER_VALIDATE_URL) - ) { - global $wpdb; - $wpdb->update( $wpdb->prefix . 'links', array ( 'link_rss' => $alternate['url'] ), array('link_id'=> $bookmark->link_id ) ); - return false; - } - } - } + if (isset($properties['uid'][0]) && !empty($properties['uid'][0])) + $title .= ' (' . $properties['uid'][0] . ')'; - $items = array (); - foreach ($mf['items'] as $topitem ) { - // look for h-feeds - if ( in_array( 'h-feed', $topitem['type'])) { - if ( !empty($topitem['children'])) { - foreach ( $topitem['children'] as $entry ) { - // look for h-entries - if ( in_array( 'h-entry', $entry['type']) ) { + $url = $properties['url'][0]; - $properties = $entry['properties']; + if ( isset($properties['updated']) && !empty($properties['updated']) ) + $date = $properties['updated'][0]; + else + $date = $properties['published'][0]; - $title = $properties['name'][0]; - $url = $properties['url'][0]; + $time = strtotime( $date ); - if ( isset($properties['updated']) && !empty($properties['updated']) ) - $date = $properties['updated'][0]; - else - $date = $properties['published'][0]; + if ( isset($properties['content'][0]['html']) && !empty($properties['content'][0]['html']) ) + $content = $properties['content'][0]['html']; + elseif ( isset($properties['summary'][0]['html']) && !empty($properties['summary'][0]['html'])) + $content = $properties['summary'][0]['html'] . '<h2><a href="'.$url.'">Read the full article &raquo;&raquo;</a></h2>'; + else + $content = '<h1><a href="'.$url.'">'.$title.'</a></h1>'; - if ( isset($properties['content'][0]['html']) && !empty($properties['content'][0]['html']) ) - $content = $properties['content'][0]['html']; - elseif ( isset($properties['summary'][0]['html']) && !empty($properties['summary'][0]['html'])) - $content = $properties['summary'][0]['html'] . '<h2><a href="'.$url.'">Read the full article &raquo;&raquo;</a></h2>'; - else - $content = '<h1><a href="'.$url.'">'.$title.'</a></h1>'; + if ( isset($properties['photo'][0]) && !empty($properties['photo'][0] ) && !stristr($content, $properties['photo'][0]) ) + $content .= '<p><img src="'.$properties['photo'][0].'" /></p>'; - $time = strtotime( $date ); + if ( isset($properties['video'][0]) && !empty($properties['video'][0] ) && !stristr($content, $properties['video'][0]) ) + $content .= '<p><video><source src="'.$properties['video'][0].'" /></video></p>'; - $item = array ( - 'url' => $url, - 'content' => $content, - 'title' => $title, - ); + $item = array ( + 'url' => $url, + 'content' => $content, + 'title' => $title, + ); - $items[ $time ] = $item; - } - } - } + $items[ $time ] = $item; } }

@@ -487,6 +518,8 @@ if ( empty( $items ) )

return false; $last_updated_ = 0; + + static::debug(" looping items"); foreach ( $items as $time => $item ) { if ( $time > $last_updated ) { if ($this->send (
M readme.txtreadme.txt

@@ -3,8 +3,8 @@ Contributors: cadeyrn

Donate link: https://paypal.me/petermolnar/3 Tags: blogroll, links, rss, email, reader Requires at least: 3.0 -Tested up to: 4.4 -Stable tag: 0.2.3 +Tested up to: 4.4.1 +Stable tag: 0.3 License: GPLv3 License URI: http://www.gnu.org/licenses/gpl-3.0.html Required minimum PHP version: 5.3

@@ -46,6 +46,11 @@

* every A. indicates BIG changes. * every .B version indicates new features. * every ..C indicates bugfixes for A.B version. + += 0.3 = +*2015-01-07* + +* support for [granary](https://github.com/snarfed/granary) JSON-MF2 format = 0.2.3 = *2015-12-12*