wp/wp-mail.php
author ymh <ymh.work@gmail.com>
Mon, 14 Oct 2019 17:39:30 +0200
changeset 7 cf61fcea0001
parent 5 5e2f62d02dcd
child 9 177826044cd9
permissions -rw-r--r--
resynchronize code repo with production
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
     1
<?php
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
     2
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
     3
 * Gets the email message from the user's mailbox to add as
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
     4
 * a WordPress post. Mailbox connection information must be
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
     5
 * configured under Settings > Writing
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
     6
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
     7
 * @package WordPress
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
     8
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
     9
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    10
/** Make sure that the WordPress bootstrap has run before continuing. */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    11
require(dirname(__FILE__) . '/wp-load.php');
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    12
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    13
/** This filter is documented in wp-admin/options.php */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    14
if ( ! apply_filters( 'enable_post_by_email_configuration', true ) )
7
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
    15
	wp_die( __( 'This action has been disabled by the administrator.' ), 403 );
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
    16
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
    17
$mailserver_url = get_option( 'mailserver_url' );
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
    18
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
    19
if ( 'mail.example.com' === $mailserver_url || empty( $mailserver_url ) ) {
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
    20
	wp_die( __( 'This action has been disabled by the administrator.' ), 403 );
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
    21
}
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    22
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    23
/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    24
 * Fires to allow a plugin to do a complete takeover of Post by Email.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    25
 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    26
 * @since 2.9.0
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    27
 */
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    28
do_action( 'wp-mail.php' );
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    29
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    30
/** Get the POP3 class with which to access the mailbox. */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    31
require_once( ABSPATH . WPINC . '/class-pop3.php' );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    32
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    33
/** Only check at this interval for new messages. */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    34
if ( !defined('WP_MAIL_INTERVAL') )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    35
	define('WP_MAIL_INTERVAL', 300); // 5 minutes
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    36
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    37
$last_checked = get_transient('mailserver_last_checked');
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    38
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    39
if ( $last_checked )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    40
	wp_die(__('Slow down cowboy, no need to check for new mails so often!'));
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    41
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    42
set_transient('mailserver_last_checked', true, WP_MAIL_INTERVAL);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    43
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    44
$time_difference = get_option('gmt_offset') * HOUR_IN_SECONDS;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    45
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    46
$phone_delim = '::';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    47
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    48
$pop3 = new POP3();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    49
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    50
if ( !$pop3->connect( get_option('mailserver_url'), get_option('mailserver_port') ) || !$pop3->user( get_option('mailserver_login') ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    51
	wp_die( esc_html( $pop3->ERROR ) );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    52
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    53
$count = $pop3->pass( get_option('mailserver_pass') );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    54
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    55
if( false === $count )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    56
	wp_die( esc_html( $pop3->ERROR ) );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    57
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    58
if( 0 === $count ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    59
	$pop3->quit();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    60
	wp_die( __('There doesn&#8217;t seem to be any new mail.') );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    61
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    62
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    63
for ( $i = 1; $i <= $count; $i++ ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    64
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    65
	$message = $pop3->get($i);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    66
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    67
	$bodysignal = false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    68
	$boundary = '';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    69
	$charset = '';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    70
	$content = '';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    71
	$content_type = '';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    72
	$content_transfer_encoding = '';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    73
	$post_author = 1;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    74
	$author_found = false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    75
	foreach ($message as $line) {
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    76
		// Body signal.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    77
		if ( strlen($line) < 3 )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    78
			$bodysignal = true;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    79
		if ( $bodysignal ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    80
			$content .= $line;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    81
		} else {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    82
			if ( preg_match('/Content-Type: /i', $line) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    83
				$content_type = trim($line);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    84
				$content_type = substr($content_type, 14, strlen($content_type) - 14);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    85
				$content_type = explode(';', $content_type);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    86
				if ( ! empty( $content_type[1] ) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    87
					$charset = explode('=', $content_type[1]);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    88
					$charset = ( ! empty( $charset[1] ) ) ? trim($charset[1]) : '';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    89
				}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    90
				$content_type = $content_type[0];
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    91
			}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    92
			if ( preg_match('/Content-Transfer-Encoding: /i', $line) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    93
				$content_transfer_encoding = trim($line);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    94
				$content_transfer_encoding = substr($content_transfer_encoding, 27, strlen($content_transfer_encoding) - 27);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    95
				$content_transfer_encoding = explode(';', $content_transfer_encoding);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    96
				$content_transfer_encoding = $content_transfer_encoding[0];
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    97
			}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    98
			if ( ( $content_type == 'multipart/alternative' ) && ( false !== strpos($line, 'boundary="') ) && ( '' == $boundary ) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    99
				$boundary = trim($line);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   100
				$boundary = explode('"', $boundary);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   101
				$boundary = $boundary[1];
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   102
			}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   103
			if (preg_match('/Subject: /i', $line)) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   104
				$subject = trim($line);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   105
				$subject = substr($subject, 9, strlen($subject) - 9);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   106
				// Captures any text in the subject before $phone_delim as the subject
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   107
				if ( function_exists('iconv_mime_decode') ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   108
					$subject = iconv_mime_decode($subject, 2, get_option('blog_charset'));
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   109
				} else {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   110
					$subject = wp_iso_descrambler($subject);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   111
				}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   112
				$subject = explode($phone_delim, $subject);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   113
				$subject = $subject[0];
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   114
			}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   115
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   116
			/*
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   117
			 * Set the author using the email address (From or Reply-To, the last used)
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   118
			 * otherwise use the site admin.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   119
			 */
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   120
			if ( ! $author_found && preg_match( '/^(From|Reply-To): /', $line ) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   121
				if ( preg_match('|[a-z0-9_.-]+@[a-z0-9_.-]+(?!.*<)|i', $line, $matches) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   122
					$author = $matches[0];
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   123
				else
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   124
					$author = trim($line);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   125
				$author = sanitize_email($author);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   126
				if ( is_email($author) ) {
7
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   127
					/* translators: Post author email address */
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   128
					echo '<p>' . sprintf(__('Author is %s'), $author) . '</p>';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   129
					$userdata = get_user_by('email', $author);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   130
					if ( ! empty( $userdata ) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   131
						$post_author = $userdata->ID;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   132
						$author_found = true;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   133
					}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   134
				}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   135
			}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   136
7
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   137
			if ( preg_match( '/Date: /i', $line ) ) { // of the form '20 Mar 2002 20:32:37 +0100'
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   138
				$ddate = str_replace( 'Date: ', '', trim( $line ) );
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   139
				$ddate = preg_replace( '!\s*\(.+\)\s*$!', '', $ddate );	// remove parenthesised timezone string if it exists, as this confuses strtotime
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   140
				$ddate_U = strtotime( $ddate );
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   141
				$post_date = gmdate( 'Y-m-d H:i:s', $ddate_U + $time_difference );
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   142
				$post_date_gmt = gmdate( 'Y-m-d H:i:s', $ddate_U );
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   143
			}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   144
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   145
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   146
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   147
	// Set $post_status based on $author_found and on author's publish_posts capability
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   148
	if ( $author_found ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   149
		$user = new WP_User($post_author);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   150
		$post_status = ( $user->has_cap('publish_posts') ) ? 'publish' : 'pending';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   151
	} else {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   152
		// Author not found in DB, set status to pending. Author already set to admin.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   153
		$post_status = 'pending';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   154
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   155
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   156
	$subject = trim($subject);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   157
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   158
	if ( $content_type == 'multipart/alternative' ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   159
		$content = explode('--'.$boundary, $content);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   160
		$content = $content[2];
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   161
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   162
		// Match case-insensitive content-transfer-encoding.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   163
		if ( preg_match( '/Content-Transfer-Encoding: quoted-printable/i', $content, $delim) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   164
			$content = explode($delim[0], $content);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   165
			$content = $content[1];
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   166
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   167
		$content = strip_tags($content, '<img><p><br><i><b><u><em><strong><strike><font><span><div>');
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   168
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   169
	$content = trim($content);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   170
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   171
	/**
7
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   172
	 * Filters the original content of the email.
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   173
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   174
	 * Give Post-By-Email extending plugins full access to the content, either
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   175
	 * the raw content, or the content of the last quoted-printable section.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   176
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   177
	 * @since 2.8.0
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   178
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   179
	 * @param string $content The original email content.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   180
	 */
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   181
	$content = apply_filters( 'wp_mail_original_content', $content );
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   182
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   183
	if ( false !== stripos($content_transfer_encoding, "quoted-printable") ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   184
		$content = quoted_printable_decode($content);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   185
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   186
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   187
	if ( function_exists('iconv') && ! empty( $charset ) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   188
		$content = iconv($charset, get_option('blog_charset'), $content);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   189
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   190
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   191
	// Captures any text in the body after $phone_delim as the body
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   192
	$content = explode($phone_delim, $content);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   193
	$content = empty( $content[1] ) ? $content[0] : $content[1];
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   194
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   195
	$content = trim($content);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   196
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   197
	/**
7
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   198
	 * Filters the content of the post submitted by email before saving.
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   199
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   200
	 * @since 1.2.0
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   201
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   202
	 * @param string $content The email content.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   203
	 */
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   204
	$post_content = apply_filters( 'phone_content', $content );
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   205
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   206
	$post_title = xmlrpc_getposttitle($content);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   207
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   208
	if ($post_title == '') $post_title = $subject;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   209
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   210
	$post_category = array(get_option('default_email_category'));
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   211
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   212
	$post_data = compact('post_content','post_title','post_date','post_date_gmt','post_author','post_category', 'post_status');
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   213
	$post_data = wp_slash($post_data);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   214
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   215
	$post_ID = wp_insert_post($post_data);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   216
	if ( is_wp_error( $post_ID ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   217
		echo "\n" . $post_ID->get_error_message();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   218
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   219
	// We couldn't post, for whatever reason. Better move forward to the next email.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   220
	if ( empty( $post_ID ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   221
		continue;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   222
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   223
	/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   224
	 * Fires after a post submitted by email is published.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   225
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   226
	 * @since 1.2.0
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   227
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   228
	 * @param int $post_ID The post ID.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   229
	 */
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   230
	do_action( 'publish_phone', $post_ID );
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   231
7
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   232
	echo "\n<p><strong>" . __( 'Author:' ) . '</strong> ' . esc_html( $post_author ) . '</p>';
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   233
	echo "\n<p><strong>" . __( 'Posted title:' ) . '</strong> ' . esc_html( $post_title ) . '</p>';
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   234
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   235
	if(!$pop3->delete($i)) {
7
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   236
		echo '<p>' . sprintf(
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   237
			/* translators: %s: POP3 error */
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   238
			__( 'Oops: %s' ),
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   239
			esc_html( $pop3->ERROR )
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   240
		) . '</p>';
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   241
		$pop3->reset();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   242
		exit;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   243
	} else {
7
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   244
		echo '<p>' . sprintf(
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   245
			/* translators: %s: the message ID */
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   246
			__( 'Mission complete. Message %s deleted.' ),
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   247
			'<strong>' . $i . '</strong>'
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   248
		) . '</p>';
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   249
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   250
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   251
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   252
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   253
$pop3->quit();