| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* Module Name: Post by email |
| 5 |
* Module Description: Publish posts by sending an email |
| 6 |
* First Introduced: 2.0 |
| 7 |
* Sort Order: 14 |
| 8 |
* Requires Connection: Yes |
| 9 |
* Auto Activate: Yes |
| 10 |
* Module Tags: Writing |
| 11 |
* Feature: Writing |
| 12 |
* Additional Search Queries: post by email, email |
| 13 |
*/ |
| 14 |
|
| 15 |
add_action( 'jetpack_modules_loaded', array( 'Jetpack_Post_By_Email', 'init' ) ); |
| 16 |
|
| 17 |
Jetpack::enable_module_configurable( __FILE__ ); |
| 18 |
Jetpack::module_configuration_load( __FILE__, array( 'Jetpack_Post_By_Email', 'configuration_redirect' ) ); |
| 19 |
|
| 20 |
class Jetpack_Post_By_Email { |
| 21 |
public static function init() { |
| 22 |
static $instance = NULL; |
| 23 |
|
| 24 |
if ( !$instance ) { |
| 25 |
$instance = new Jetpack_Post_By_Email; |
| 26 |
} |
| 27 |
|
| 28 |
return $instance; |
| 29 |
} |
| 30 |
|
| 31 |
function __construct() { |
| 32 |
add_action( 'init', array( &$this, 'action_init' ) ); |
| 33 |
} |
| 34 |
|
| 35 |
static function configuration_redirect() { |
| 36 |
wp_safe_redirect( get_edit_profile_url( get_current_user_id() ) . '#post-by-email' ); |
| 37 |
exit; |
| 38 |
} |
| 39 |
|
| 40 |
function action_init() { |
| 41 |
if ( ! current_user_can( 'edit_posts' ) ) |
| 42 |
return; |
| 43 |
|
| 44 |
add_action( 'profile_personal_options', array( &$this, 'user_profile' ) ); |
| 45 |
add_action( 'admin_print_scripts-profile.php', array( &$this, 'profile_scripts' ) ); |
| 46 |
|
| 47 |
add_action( 'wp_ajax_jetpack_post_by_email_enable', array( &$this, 'create_post_by_email_address' ) ); |
| 48 |
add_action( 'wp_ajax_jetpack_post_by_email_regenerate', array( &$this, 'regenerate_post_by_email_address' ) ); |
| 49 |
add_action( 'wp_ajax_jetpack_post_by_email_disable', array( &$this, 'delete_post_by_email_address' ) ); |
| 50 |
} |
| 51 |
|
| 52 |
function profile_scripts() { |
| 53 |
wp_enqueue_script( 'post-by-email', plugins_url( 'post-by-email/post-by-email.js', __FILE__ ), array( 'jquery' ) ); |
| 54 |
wp_localize_script( 'post-by-email', 'pbeVars', array( |
| 55 |
'nonces' => array( |
| 56 |
'enable' => wp_create_nonce( 'jetpack.createPostByEmailAddress' ), |
| 57 |
'regenerate' => wp_create_nonce( 'jetpack.regeneratePostByEmailAddress' ), |
| 58 |
'disable' => wp_create_nonce( 'jetpack.deletePostByEmailAddress' ), |
| 59 |
), |
| 60 |
)); |
| 61 |
wp_enqueue_style( 'post-by-email', plugins_url( 'post-by-email/post-by-email.css', __FILE__ ) ); |
| 62 |
wp_style_add_data( 'post-by-email', 'jetpack-inline', true ); |
| 63 |
// Do we really need `admin_styles`? With the new admin UI, it's breaking some bits. |
| 64 |
// Jetpack::init()->admin_styles(); |
| 65 |
} |
| 66 |
|
| 67 |
function check_user_connection() { |
| 68 |
$user_token = Jetpack_Data::get_access_token( get_current_user_id() ); |
| 69 |
$is_user_connected = $user_token && !is_wp_error( $user_token ); |
| 70 |
|
| 71 |
// If the user is already connected via Jetpack, then we're good |
| 72 |
if ( $is_user_connected ) |
| 73 |
return true; |
| 74 |
|
| 75 |
return false; |
| 76 |
} |
| 77 |
|
| 78 |
function user_profile() { |
| 79 |
$blog_name = get_bloginfo( 'blogname' ); |
| 80 |
if ( empty( $blog_name ) ) { |
| 81 |
$blog_name = home_url( '/' ); |
| 82 |
} |
| 83 |
|
| 84 |
?> |
| 85 |
<div id="post-by-email" class="jetpack-targetable"> |
| 86 |
<h3><?php esc_html_e( 'Post by Email', 'jetpack' ); ?></h3> |
| 87 |
<table class="form-table"> |
| 88 |
<tr> |
| 89 |
<th scope="row"><?php esc_html_e( 'Email Address', 'jetpack' ); ?><span id="jp-pbe-spinner" class="spinner"></span></th> |
| 90 |
<td> |
| 91 |
<div id="jp-pbe-error" class="jetpack-inline-error"></div> <?php |
| 92 |
|
| 93 |
if ( $this->check_user_connection() ) { |
| 94 |
$email = $this->get_post_by_email_address(); |
| 95 |
|
| 96 |
if ( empty( $email ) ) { |
| 97 |
$enable_hidden = ''; |
| 98 |
$info_hidden = ' style="display: none;"'; |
| 99 |
} else { |
| 100 |
$enable_hidden = ' style="display: none;"'; |
| 101 |
$info_hidden = ''; |
| 102 |
} ?> |
| 103 |
|
| 104 |
<input type="button" name="jp-pbe-enable" id="jp-pbe-enable" class="button" value="<?php esc_attr_e( 'Enable Post By Email', 'jetpack' ); ?> "<?php echo $enable_hidden; ?> /> |
| 105 |
<div id="jp-pbe-info"<?php echo $info_hidden; ?>> |
| 106 |
<p id="jp-pbe-email-wrapper"> |
| 107 |
<input type="text" id="jp-pbe-email" value="<?php echo esc_attr( $email ); ?>" readonly="readonly" class="regular-text" /> |
| 108 |
<span class="description"><a target="_blank" href="http://jetpack.com/support/post-by-email/"><?php esc_html_e( 'More information', 'jetpack' ); ?></a></span> |
| 109 |
</p> |
| 110 |
<p> |
| 111 |
<input type="button" name="jp-pbe-regenerate" id="jp-pbe-regenerate" class="button" value="<?php esc_attr_e( 'Regenerate Address', 'jetpack' ); ?> " /> |
| 112 |
<input type="button" name="jp-pbe-disable" id="jp-pbe-disable" class="button" value="<?php esc_attr_e( 'Disable Post By Email', 'jetpack' ); ?> " /> |
| 113 |
</p> |
| 114 |
</div> <?php |
| 115 |
} else { |
| 116 |
$jetpack = Jetpack::init(); ?> |
| 117 |
|
| 118 |
<p class="jetpack-inline-message"> |
| 119 |
<?php printf( |
| 120 |
esc_html( wptexturize( __( 'To use Post By Email, you need to link your %s account to your WordPress.com account.', 'jetpack' ) ) ), |
| 121 |
'<strong>' . esc_html( $blog_name ) . '</strong>' |
| 122 |
); ?><br /> |
| 123 |
<?php echo esc_html( wptexturize( __( "If you don't have a WordPress.com account yet, you can sign up for free in just a few seconds.", 'jetpack' ) ) ); ?> |
| 124 |
</p> |
| 125 |
<p> |
| 126 |
<a href="<?php echo $jetpack->build_connect_url( false, get_edit_profile_url( get_current_user_id() ) . '#post-by-email', 'unlinked-user-pbe' ); ?>" class="button button-connector" id="wpcom-connect"><?php esc_html_e( 'Link account with WordPress.com', 'jetpack' ); ?></a> |
| 127 |
</p> |
| 128 |
<?php |
| 129 |
} ?> |
| 130 |
</td> |
| 131 |
</tr> |
| 132 |
</table> |
| 133 |
</div> |
| 134 |
<?php |
| 135 |
} |
| 136 |
|
| 137 |
function get_post_by_email_address() { |
| 138 |
Jetpack::load_xml_rpc_client(); |
| 139 |
$xml = new Jetpack_IXR_Client( array( |
| 140 |
'user_id' => get_current_user_id(), |
| 141 |
) ); |
| 142 |
$xml->query( 'jetpack.getPostByEmailAddress' ); |
| 143 |
|
| 144 |
if ( $xml->isError() ) |
| 145 |
return NULL; |
| 146 |
|
| 147 |
$response = $xml->getResponse(); |
| 148 |
if ( empty( $response ) ) |
| 149 |
return NULL; |
| 150 |
|
| 151 |
return $response; |
| 152 |
} |
| 153 |
|
| 154 |
function create_post_by_email_address() { |
| 155 |
self::__process_ajax_proxy_request( |
| 156 |
'jetpack.createPostByEmailAddress', |
| 157 |
__( 'Unable to create your Post By Email address. Please try again later.', 'jetpack' ) |
| 158 |
); |
| 159 |
} |
| 160 |
|
| 161 |
function regenerate_post_by_email_address() { |
| 162 |
self::__process_ajax_proxy_request( |
| 163 |
'jetpack.regeneratePostByEmailAddress', |
| 164 |
__( 'Unable to regenerate your Post By Email address. Please try again later.', 'jetpack' ) |
| 165 |
); |
| 166 |
} |
| 167 |
|
| 168 |
function delete_post_by_email_address() { |
| 169 |
self::__process_ajax_proxy_request( |
| 170 |
'jetpack.deletePostByEmailAddress', |
| 171 |
__( 'Unable to disable your Post By Email address. Please try again later.', 'jetpack' ) |
| 172 |
); |
| 173 |
} |
| 174 |
|
| 175 |
/** |
| 176 |
* Back end function to abstract the xmlrpc function calls to wpcom. |
| 177 |
* |
| 178 |
* @param $endpoint |
| 179 |
* @param $error_message |
| 180 |
*/ |
| 181 |
function __process_ajax_proxy_request( $endpoint, $error_message ) { |
| 182 |
if ( ! current_user_can( 'edit_posts' ) ) { |
| 183 |
wp_send_json_error( $error_message ); |
| 184 |
} |
| 185 |
if ( empty( $_REQUEST['pbe_nonce'] ) || ! wp_verify_nonce( $_REQUEST['pbe_nonce'], $endpoint ) ) { |
| 186 |
wp_send_json_error( $error_message ); |
| 187 |
} |
| 188 |
Jetpack::load_xml_rpc_client(); |
| 189 |
$xml = new Jetpack_IXR_Client( array( |
| 190 |
'user_id' => get_current_user_id(), |
| 191 |
) ); |
| 192 |
$xml->query( $endpoint ); |
| 193 |
|
| 194 |
if ( $xml->isError() ) { |
| 195 |
wp_send_json_error( $error_message ); |
| 196 |
} |
| 197 |
|
| 198 |
$response = $xml->getResponse(); |
| 199 |
if ( empty( $response ) ) { |
| 200 |
wp_send_json_error( $error_message ); |
| 201 |
} |
| 202 |
|
| 203 |
// Will be used only in Jetpack_Core_Json_Api_Endpoints::get_remote_value. |
| 204 |
update_option( 'post_by_email_address' . get_current_user_id(), $response ); |
| 205 |
|
| 206 |
wp_send_json_success( $response ); |
| 207 |
} |
| 208 |
} |
| 209 |
|