all repos — blogroll2email @ a89bb3d5f9dd522075d07734892770b44ecb4d1a

blogroll2email.php (view raw)

 1
 2
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
 100
 101
 102
 103
 104
 105
 106
 107
 108
 109
 110
 111
 112
 113
 114
 115
 116
 117
 118
 119
 120
 121
 122
 123
 124
 125
 126
 127
 128
 129
 130
 131
 132
 133
 134
 135
 136
 137
 138
 139
 140
 141
 142
 143
 144
 145
 146
 147
 148
 149
 150
 151
 152
 153
 154
 155
 156
 157
 158
 159
 160
 161
 162
 163
 164
 165
 166
 167
 168
 169
 170
 171
 172
 173
 174
 175
 176
 177
 178
 179
 180
 181
 182
 183
 184
 185
 186
 187
 188
 189
 190
 191
 192
 193
 194
 195
 196
 197
 198
 199
 200
 201
 202
 203
 204
 205
 206
 207
 208
 209
 210
 211
 212
 213
 214
 215
 216
 217
 218
 219
 220
 221
 222
 223
 224
 225
 226
 227
 228
 229
 230
 231
 232
 233
 234
 235
 236
 237
 238
 239
 240
 241
 242
 243
 244
 245
 246
 247
 248
 249
 250
 251
 252
 253
 254
 255
 256
 257
 258
 259
 260
 261
 262
 263
 264
 265
 266
 267
 268
 269
 270
 271
 272
 273
 274
 275
 276
 277
 278
 279
 280
 281
 282
 283
 284
 285
 286
 287
 288
 289
 290
 291
 292
 293
 294
 295
 296
 297
 298
 299
 300
 301
 302
 303
 304
 305
 306
 307
 308
 309
 310
 311
 312
 313
 314
 315
 316
 317
 318
 319
 320
 321
 322
 323
 324
 325
 326
 327
 328
 329
 330
 331
 332
 333
 334
 335
 336
 337
 338
 339
 340
 341
 342
 343
 344
 345
 346
 347
 348
 349
 350
 351
 352
 353
 354
 355
 356
 357
 358
 359
 360
 361
 362
 363
 364
 365
 366
 367
 368
 369
 370
 371
 372
 373
 374
 375
 376
 377
 378
 379
 380
 381
 382
 383
 384
 385
 386
 387
 388
 389
 390
 391
 392
 393
 394
 395
 396
 397
<?php

/*
Plugin Name: blogroll2email
Plugin URI: https://github.com/petermolnar/blogroll2email
Description: Pulls RSS, Atom and microformats entries from blogroll links and sends them as email
Version: 0.1
Author: Peter Molnar <hello@petermolnar.eu>
Author URI: http://petermolnar.eu/
License: GPLv3
*/

/*  Copyright 2015 Peter Molnar ( hello@petermolnar.eu )

	This program is free software; you can redistribute it and/or modify
	it under the terms of the GNU General Public License, version 3, as
	published by the Free Software Foundation.

	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.

	You should have received a copy of the GNU General Public License
	along with this program; if not, write to the Free Software
	Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
*/

class blogroll2email {
	const revisit_time = 1800;
	const schedule = 'blogroll2email';

	var $schedule = null;

	public function __construct () {
		register_activation_hook( __FILE__ , array( &$this, 'plugin_activate' ) );
		register_deactivation_hook( __FILE__ , array( &$this, 'plugin_deactivate' ) );
		add_action( 'init', array( &$this, 'init'));
		add_filter( 'cron_schedules', array(&$this, 'add_cron_schedule' ));
	}

	public function init () {
		// additional cron schedules
		add_action( static::schedule, array( &$this, 'worker' ) );
		if (!wp_get_schedule( static::schedule ))
			wp_schedule_event ( time(), static::schedule, static::schedule );
		return false;
	}

	public function add_cron_schedule ( $schedules ) {

		$schedules[ static::schedule ] = array(
			'interval' => static::revisit_time,
			'display' => sprintf(__( 'every %d seconds' ), static::revisit_time )
		);

		return $schedules;
	}


	/**
	 * activation hook function, to be extended
	 */
	public function plugin_activate() {
		self::debug('activating');

	}

	/**
	 * deactivation hook function, to be extended
	 */
	public function plugin_deactivate () {
		self::debug('deactivating');
		wp_unschedule_event( time(), static::schedule );
		wp_clear_scheduled_hook( static::schedule );
	}

	public function worker () {
		self::debug('worker started');
		$args = array(
			'orderby' => 'owner',
			'order' => 'ASC',
			'limit' => -1,
		);

		$bookmarks = get_bookmarks( $args );

		$currowner = $owner = false;

		foreach ( $bookmarks as $bookmark ) {
			/* print_r ($bookmark);
				stdClass Object
				(
					[link_id] => 1
					[link_url] => http://devopsreactions.tumblr.com/
					[link_name] => http://devopsreactions.tumblr.com/
					[link_image] =>
					[link_target] =>
					[link_description] =>
					[link_visible] => Y
					[link_owner] => 1
					[link_rating] => 0
					[link_updated] => 0000-00-00 00:00:00
					[link_rel] =>
					[link_notes] =>
					[link_rss] => http://devopsreactions.tumblr.com/rss
				)
			*/

			if ( $currowner != $bookmark->link_owner) {
				$currowner = $bookmark->link_owner;
				$owner = get_userdata($currowner);
				$owner = $owner->data;
				/* print_r ( $owner );
				stdClass Object
				(
					[ID] => 1
					[user_login] => cadeyrn
					[user_pass] => $P$B.QI9GiDNyfXC7S75jJ4pcrQjx3awy/
					[user_nicename] => cadeyrn
					[user_email] => hello@petermolnar.eu
					[user_url] => https://petermolnar.eu/
					[user_registered] => 2014-04-28 21:36:35
					[user_activation_key] =>
					[user_status] => 0
					[display_name] => Peter Molnar
				)
				*/
			}

			if ( empty($bookmark->link_rss))
				$this->parse_mf ( $bookmark, $owner );
			else
				$this->parse_rss( $bookmark, $owner );
		}

		// reschedule the worker
		wp_schedule_single_event ( time() + static::revisit_time, static::schedule );
	}

	/**
	 *
	 */
	public function set_html_content_type() {
		return 'text/html';
	}

	/**
	 *
	 */
	protected function send ( $to, $link, $title, $fromname, $sourceurl, $content ) {

		add_filter( 'wp_mail_content_type', array( $this, 'set_html_content_type') );

		$body = '<html><head></head><h1><a href="'. $link .'">'. $title .'</a></h1><body>'. $content . '<p>URL: <a href="'.$link.'">'.$link.'</a></p></body></html>';

		$sitedomain = parse_url( get_bloginfo('url'), PHP_URL_HOST);

		$headers = array (
			'X-RSS-ID: ' . $link,
			'X-RSS-URL: ' . $link,
			'X-RSS-Feed: ' . $sourceurl,
			'User-Agent: blogroll2email',
			'From: "' . $fromname .'" <'. static::schedule . '@'. $sitedomain .'>',
		);

		self::debug('sending ' . $title . ' to ' . $to );
		wp_mail( $to, $title, $body, $headers );

		remove_filter( 'wp_mail_content_type', array( $this, 'set_html_content_type') );
	}

	/**
	 *
	 */
	protected function parse_rss ( $bookmark, $owner ) {

		if ( empty ($bookmark) || !is_object ($bookmark))
			return false;

		if ( empty ($owner) || !is_object ($owner))
			return false;

		if ( !class_exists('SimplePie') )
			require_once( ABSPATH . WPINC . '/class-simplepie.php' );

		$url = $bookmark->link_rss;
		$last_updated = strtotime( $bookmark->link_updated );

		error_log('Fetching: ' . $url );
		$feed = new SimplePie();
		$feed->set_feed_url( $url );
		$feed->set_cache_duration ( static::revisit_time );
		$feed->set_cache_location( WP_CONTENT_DIR . DIRECTORY_SEPARATOR. 'cache' );
		$feed->force_feed(true);

		// optimization
		$feed->enable_order_by_date(true);
		$feed->remove_div(false);
		$feed->strip_comments(true);
		$feed->strip_htmltags(false);
		$feed->strip_attributes(false);
		$feed->set_image_handler(false);

		$feed->init();
		$feed->handle_content_type();
		if ( $feed->error() )
			return new WP_Error( 'simplepie-error', $feed->error() );

		$maxitems = $feed->get_item_quantity( 12 );
		$feed_items = $feed->get_items( 0, $maxitems );
		$feed_title = $feed->get_title();

		if ( !empty($feed_title) && $bookmark->link_name != $feed_title ) {
			global $wpdb;
			$wpdb->update( $wpdb->prefix . 'links', array ( 'link_name' => $feed_title ), array('link_id'=> $bookmark->link_id ) );
		}

		$feed_author = $feed->get_author();

		if ( $maxitems > 0 ) {
			$last_updated_ = 0;
			foreach ( $feed_items as $item ) {
				$date = $item->get_date( 'U' );

				if ( $date > $last_updated ) {
					$from = $feed_title;
					$author = $item->get_author();
					if ($author)
						$from = $from . ': ' . $author->get_name();
					elseif ( $feed_author )
						$from = $from . ': ' . $feed_author->get_name();

					$this->send (
						$owner->user_email,
						$item->get_link(),
						$item->get_title(),
						$from,
						$url,
						$item->get_content()
					);

					if ( $date > $last_updated_ )
						$last_updated_ = $date;
				}
			}
		}

		$this->update_link_date ( $bookmark, $last_updated_ );
	}


	/***
	 *
	 */
	protected function parse_mf ( $bookmark, $owner ) {

		if ( empty ($bookmark) || !is_object ($bookmark))
			return false;

		if ( empty ($owner) || !is_object ($owner))
			return false;

		if ( !class_exists('Mf2') )
			require_once ( dirname(__FILE__) . '/lib/php-mf2/Mf2/Parser.php' );

		$last_updated = strtotime( $bookmark->link_updated );

		$url = $bookmark->link_url;

		//$hash = md5( $url );
		// check cache file and skip this step if exists ?

		$mf = Mf2\fetch($url,  true, $curlInfo);
		error_log('MF2 fetching: ' . $url );

		// 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;
					}
			}
		}

		$item = array ();

		foreach ($mf['items'] as $topitem ) {
			if ( in_array( 'h-feed', $topitem['type'])) {
				if ( !empty($topitem['children'])) {
					foreach ( $topitem['children'] as $entry ) {
						if ( in_array( 'h-entry', $entry['type']) ) {

							$properties = $entry['properties'];

							$title = $properties['name'][0];
							$url = $properties['url'][0];

							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>';

							$time = strtotime( $date );

							$item = array (
								'url' => $url,
								'content' => $content,
								//'date' => $date,
								'title' => $title,
								//'author' =>
							);

							$items[ $time ] = $item;
						}
					}
				}
			}
		}

		if ( empty( $items ) )
			return false;

		$last_updated_ = 0;
		foreach ( $items as $time => $item ) {

			if ( $time > $last_updated ) {
				$this->send (
					$owner->user_email,
					$item['url'],
					$item['title'],
					$bookmark->link_name,
					$item['url'],
					$item['content']
				);

				if ( $time > $last_updated_ )
					$last_updated_ = $time;
			}
		}

		$this->update_link_date ( $bookmark, $last_updated_ );
	}

	/**
	 *
	 */
	protected function update_link_date ( $bookmark, $last_updated ) {
		$current_updated = strtotime( $bookmark->link_updated );
		if ( $last_updated > $current_updated ) {
			global $wpdb;
			$wpdb->update( $wpdb->prefix . 'links', array ( 'link_updated' => date( 'Y-m-d H:i:s', $last_updated )), array('link_id'=> $bookmark->link_id ) );
		}
	}

	/**
	 *
	 */
	static function debug( $message, $level = LOG_NOTICE ) {
		if ( @is_array( $message ) || @is_object ( $message ) )
			$message = json_encode($message);


		switch ( $level ) {
			case LOG_ERR :
				wp_die( '<h1>Error:</h1>' . '<p>' . $message . '</p>' );
				exit;
			default:
				if ( !defined( 'WP_DEBUG' ) || WP_DEBUG != true )
					return;
				break;
		}

		error_log(  __CLASS__ . ": " . $message );
	}


}

$blogroll2email = new blogroll2email();