/modules/post-by-email.php

  1. <?php 
  2.  
  3. /** 
  4. * Module Name: Post by Email 
  5. * Module Description: Publish posts by email, using any device and email client. 
  6. * First Introduced: 2.0 
  7. * Sort Order: 14 
  8. * Requires Connection: Yes 
  9. * Auto Activate: Yes 
  10. * Module Tags: Writing 
  11. * Additional Search Queries: post by email, email 
  12. */ 
  13.  
  14. add_action( 'jetpack_modules_loaded', array( 'Jetpack_Post_By_Email', 'init' ) ); 
  15.  
  16. Jetpack_Sync::sync_options( __FILE__,  
  17. 'large_size_w',  
  18. 'large_size_h',  
  19. 'thumbnail_size_w',  
  20. 'thumbnail_size_h',  
  21. 'medium_size_w',  
  22. 'medium_size_h' 
  23. ); 
  24.  
  25. add_action( 'jetpack_activate_module_post-by-email', array( 'Jetpack_Post_By_Email', 'module_toggle' ) ); 
  26. add_action( 'jetpack_deactivate_module_post-by-email', array( 'Jetpack_Post_By_Email', 'module_toggle' ) ); 
  27.  
  28. Jetpack::enable_module_configurable( __FILE__ ); 
  29. Jetpack::module_configuration_load( __FILE__, array( 'Jetpack_Post_By_Email', 'configuration_redirect' ) ); 
  30.  
  31. class Jetpack_Post_By_Email { 
  32. public static function init() { 
  33. static $instance = NULL; 
  34.  
  35. if ( !$instance ) { 
  36. $instance = new Jetpack_Post_By_Email; 
  37.  
  38. return $instance; 
  39.  
  40. function __construct() { 
  41. add_action( 'init', array( &$this, 'action_init' ) ); 
  42.  
  43. static function module_toggle() { 
  44. $jetpack = Jetpack::init(); 
  45. $jetpack->sync->register( 'noop' ); 
  46.  
  47. static function configuration_redirect() { 
  48. wp_safe_redirect( get_edit_profile_url( get_current_user_id() ) . '#post-by-email' ); 
  49. exit; 
  50.  
  51. function action_init() { 
  52. if ( ! current_user_can( 'edit_posts' ) ) 
  53. return; 
  54.  
  55. add_action( 'profile_personal_options', array( &$this, 'user_profile' ) ); 
  56. add_action( 'admin_print_scripts-profile.php', array( &$this, 'profile_scripts' ) ); 
  57.  
  58. add_action( 'wp_ajax_jetpack_post_by_email_enable', array( &$this, 'create_post_by_email_address' ) ); 
  59. add_action( 'wp_ajax_jetpack_post_by_email_regenerate', array( &$this, 'regenerate_post_by_email_address' ) ); 
  60. add_action( 'wp_ajax_jetpack_post_by_email_disable', array( &$this, 'delete_post_by_email_address' ) ); 
  61.  
  62. function profile_scripts() { 
  63. wp_enqueue_script( 'post-by-email', plugins_url( 'post-by-email/post-by-email.js', __FILE__ ), array( 'jquery' ) ); 
  64. wp_enqueue_style( 'post-by-email', plugins_url( 'post-by-email/post-by-email.css', __FILE__ ) ); 
  65. wp_style_add_data( 'post-by-email', 'jetpack-inline', true ); 
  66. // Do we really need `admin_styles`? With the new admin UI, it's breaking some bits. 
  67. // Jetpack::init()->admin_styles(); 
  68.  
  69. function check_user_connection() { 
  70. $user_token = Jetpack_Data::get_access_token( get_current_user_id() ); 
  71. $is_user_connected = $user_token && !is_wp_error( $user_token ); 
  72.  
  73. // If the user is already connected via Jetpack, then we're good 
  74. if ( $is_user_connected ) 
  75. return true; 
  76.  
  77. return false; 
  78.  
  79. function user_profile() { 
  80. $blog_name = get_bloginfo( 'blogname' ); 
  81. if ( empty( $blog_name ) ) { 
  82. $blog_name = home_url( '/' ); 
  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.me/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' ); ?>" 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. function get_post_by_email_address() { 
  137. Jetpack::load_xml_rpc_client(); 
  138. $xml = new Jetpack_IXR_Client( array( 
  139. 'user_id' => get_current_user_id(),  
  140. ) ); 
  141. $xml->query( 'jetpack.getPostByEmailAddress' ); 
  142.  
  143. if ( $xml->isError() ) 
  144. return NULL; 
  145.  
  146. $response = $xml->getResponse(); 
  147. if ( empty( $response ) ) 
  148. return NULL; 
  149.  
  150. return $response; 
  151.  
  152. function create_post_by_email_address() { 
  153. Jetpack::load_xml_rpc_client(); 
  154. $xml = new Jetpack_IXR_Client( array( 
  155. 'user_id' => get_current_user_id(),  
  156. ) ); 
  157. $xml->query( 'jetpack.createPostByEmailAddress' ); 
  158.  
  159. if ( $xml->isError() ) { 
  160. echo json_encode( array( 
  161. 'response' => 'error',  
  162. 'message' => __( 'Unable to create your Post By Email address. Please try again later.', 'jetpack' ) 
  163. ) ); 
  164. die(); 
  165.  
  166. $response = $xml->getResponse(); 
  167. if ( empty( $response ) ) { 
  168. echo json_encode( array( 
  169. 'response' => 'error',  
  170. 'message' => __( 'Unable to create your Post By Email address. Please try again later.', 'jetpack' ) 
  171. ) ); 
  172. die(); 
  173.  
  174. echo $response; 
  175. die(); 
  176.  
  177. function regenerate_post_by_email_address() { 
  178. Jetpack::load_xml_rpc_client(); 
  179. $xml = new Jetpack_IXR_Client( array( 
  180. 'user_id' => get_current_user_id(),  
  181. ) ); 
  182. $xml->query( 'jetpack.regeneratePostByEmailAddress' ); 
  183.  
  184. if ( $xml->isError() ) { 
  185. echo json_encode( array( 
  186. 'response' => 'error',  
  187. 'message' => __( 'Unable to regenerate your Post By Email address. Please try again later.', 'jetpack' ) 
  188. ) ); 
  189. die(); 
  190.  
  191. $response = $xml->getResponse(); 
  192. if ( empty( $response ) ) { 
  193. echo json_encode( array( 
  194. 'response' => 'error',  
  195. 'message' => __( 'Unable to regenerate your Post By Email address. Please try again later.', 'jetpack' ) 
  196. ) ); 
  197. die(); 
  198.  
  199. echo $response; 
  200. die(); 
  201.  
  202. function delete_post_by_email_address() { 
  203. Jetpack::load_xml_rpc_client(); 
  204. $xml = new Jetpack_IXR_Client( array( 
  205. 'user_id' => get_current_user_id(),  
  206. ) ); 
  207. $xml->query( 'jetpack.deletePostByEmailAddress' ); 
  208.  
  209. if ( $xml->isError() ) { 
  210. echo json_encode( array( 
  211. 'response' => 'error',  
  212. 'message' => __( 'Unable to disable your Post By Email address. Please try again later.', 'jetpack' ) 
  213. ) ); 
  214. die(); 
  215.  
  216. $response = $xml->getResponse(); 
  217. if ( empty( $response ) ) { 
  218. echo json_encode( array( 
  219. 'response' => 'error',  
  220. 'message' => __( 'Unable to disable your Post By Email address. Please try again later.', 'jetpack' ) 
  221. ) ); 
  222. die(); 
  223.  
  224. echo $response; 
  225. die(); 
.