post-by-email.php 7.0 KB

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