| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* Module Name: Post by Email |
| 5 |
* Module Description: Publish posts to your blog directly from your personal email account. |
| 6 |
* First Introduced: 2.0 |
| 7 |
* Sort Order: 4 |
| 8 |
* Requires Connection: Yes |
| 9 |
* Auto Activate: Yes |
| 10 |
* Module Tags: Writing |
| 11 |
*/ |
| 12 |
|
| 13 |
add_action( 'jetpack_modules_loaded', array( 'Jetpack_Post_By_Email', 'init' ) ); |
| 14 |
|
| 15 |
Jetpack_Sync::sync_options( __FILE__, |
| 16 |
'large_size_w', |
| 17 |
'large_size_h', |
| 18 |
'thumbnail_size_w', |
| 19 |
'thumbnail_size_h', |
| 20 |
'medium_size_w', |
| 21 |
'medium_size_h' |
| 22 |
); |
| 23 |
|
| 24 |
add_action( 'jetpack_activate_module_post-by-email', array( 'Jetpack_Post_By_Email', 'module_toggle' ) ); |
| 25 |
add_action( 'jetpack_deactivate_module_post-by-email', array( 'Jetpack_Post_By_Email', 'module_toggle' ) ); |
| 26 |
|
| 27 |
Jetpack::enable_module_configurable( __FILE__ ); |
| 28 |
Jetpack::module_configuration_load( __FILE__, array( 'Jetpack_Post_By_Email', 'configuration_redirect' ) ); |
| 29 |
|
| 30 |
class Jetpack_Post_By_Email { |
| 31 |
public static function init() { |
| 32 |
static $instance = NULL; |
| 33 |
|
| 34 |
if ( !$instance ) { |
| 35 |
$instance = new Jetpack_Post_By_Email; |
| 36 |
} |
| 37 |
|
| 38 |
return $instance; |
| 39 |
} |
| 40 |
|
| 41 |
function __construct() { |
| 42 |
add_action( 'init', array( &$this, 'action_init' ) ); |
| 43 |
} |
| 44 |
|
| 45 |
function module_toggle() { |
| 46 |
$jetpack = Jetpack::init(); |
| 47 |
$jetpack->sync->register( 'noop' ); |
| 48 |
} |
| 49 |
|
| 50 |
static function configuration_redirect() { |
| 51 |
wp_safe_redirect( get_edit_profile_url( get_current_user_id() ) . '#post-by-email' ); |
| 52 |
exit; |
| 53 |
} |
| 54 |
|
| 55 |
function action_init() { |
| 56 |
if ( ! current_user_can( 'edit_posts' ) ) |
| 57 |
return; |
| 58 |
|
| 59 |
add_action( 'profile_personal_options', array( &$this, 'user_profile' ) ); |
| 60 |
add_action( 'admin_print_scripts-profile.php', array( &$this, 'profile_scripts' ) ); |
| 61 |
|
| 62 |
add_action( 'wp_ajax_jetpack_post_by_email_enable', array( &$this, 'create_post_by_email_address' ) ); |
| 63 |
add_action( 'wp_ajax_jetpack_post_by_email_regenerate', array( &$this, 'regenerate_post_by_email_address' ) ); |
| 64 |
add_action( 'wp_ajax_jetpack_post_by_email_disable', array( &$this, 'delete_post_by_email_address' ) ); |
| 65 |
} |
| 66 |
|
| 67 |
function profile_scripts() { |
| 68 |
wp_enqueue_script( 'post-by-email', plugins_url( 'post-by-email/post-by-email.js', __FILE__ ), array( 'jquery' ) ); |
| 69 |
wp_enqueue_style( 'post-by-email', plugins_url( 'post-by-email/post-by-email.css', __FILE__ ) ); |
| 70 |
Jetpack::init()->admin_styles(); |
| 71 |
} |
| 72 |
|
| 73 |
function check_user_connection() { |
| 74 |
$user_token = Jetpack_Data::get_access_token( get_current_user_id() ); |
| 75 |
$is_user_connected = $user_token && !is_wp_error( $user_token ); |
| 76 |
|
| 77 |
// If the user is already connected via Jetpack, then we're good |
| 78 |
if ( $is_user_connected ) |
| 79 |
return true; |
| 80 |
|
| 81 |
return false; |
| 82 |
} |
| 83 |
|
| 84 |
function user_profile() { |
| 85 |
$blog_name = get_bloginfo( 'blogname' ); |
| 86 |
if ( empty( $blog_name ) ) { |
| 87 |
$blog_name = home_url( '/' ); |
| 88 |
} |
| 89 |
|
| 90 |
?> |
| 91 |
<div id="post-by-email" class="jetpack-targetable"> |
| 92 |
<h3><?php esc_html_e( 'Post by Email', 'jetpack' ); ?></h3> |
| 93 |
<table class="form-table"> |
| 94 |
<tr> |
| 95 |
<th scope="row"><?php esc_html_e( 'Email Address', 'jetpack' ); ?><span id="jp-pbe-spinner" class="spinner"></span></th> |
| 96 |
<td> |
| 97 |
<div id="jp-pbe-error" class="jetpack-inline-error"></div> <?php |
| 98 |
|
| 99 |
if ( $this->check_user_connection() ) { |
| 100 |
$email = $this->get_post_by_email_address(); |
| 101 |
|
| 102 |
if ( empty( $email ) ) { |
| 103 |
$enable_hidden = ''; |
| 104 |
$info_hidden = ' style="display: none;"'; |
| 105 |
} else { |
| 106 |
$enable_hidden = ' style="display: none;"'; |
| 107 |
$info_hidden = ''; |
| 108 |
} ?> |
| 109 |
|
| 110 |
<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; ?> /> |
| 111 |
<div id="jp-pbe-info"<?php echo $info_hidden; ?>> |
| 112 |
<p id="jp-pbe-email-wrapper"> |
| 113 |
<input type="text" id="jp-pbe-email" value="<?php echo esc_attr( $email ); ?>" readonly="readonly" class="regular-text" /> |
| 114 |
<span class="description"><a target="_blank" href="http://jetpack.me/support/post-by-email/"><?php esc_html_e( 'More information', 'jetpack' ); ?></a></span> |
| 115 |
</p> |
| 116 |
<p> |
| 117 |
<input type="button" name="jp-pbe-regenerate" id="jp-pbe-regenerate" class="button" value="<?php esc_attr_e( 'Regenerate Address', 'jetpack' ); ?> " /> |
| 118 |
<input type="button" name="jp-pbe-disable" id="jp-pbe-disable" class="button" value="<?php esc_attr_e( 'Disable Post By Email', 'jetpack' ); ?> " /> |
| 119 |
</p> |
| 120 |
</div> <?php |
| 121 |
} else { |
| 122 |
$jetpack = Jetpack::init(); ?> |
| 123 |
|
| 124 |
<p class="jetpack-inline-message"> |
| 125 |
<?php printf( |
| 126 |
esc_html( wptexturize( __( 'To use Post By Email, you need to link your %s account to your WordPress.com account.', 'jetpack' ) ) ), |
| 127 |
'<strong>' . esc_html( $blog_name ) . '</strong>' |
| 128 |
); ?><br /> |
| 129 |
<?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' ) ) ); ?> |
| 130 |
</p> |
| 131 |
<p> |
| 132 |
<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> |
| 133 |
</p> |
| 134 |
<?php |
| 135 |
} ?> |
| 136 |
</td> |
| 137 |
</tr> |
| 138 |
</table> |
| 139 |
</div> |
| 140 |
<?php |
| 141 |
} |
| 142 |
|
| 143 |
function get_post_by_email_address() { |
| 144 |
Jetpack::load_xml_rpc_client(); |
| 145 |
$xml = new Jetpack_IXR_Client( array( |
| 146 |
'user_id' => get_current_user_id(), |
| 147 |
) ); |
| 148 |
$xml->query( 'jetpack.getPostByEmailAddress' ); |
| 149 |
|
| 150 |
if ( $xml->isError() ) |
| 151 |
return NULL; |
| 152 |
|
| 153 |
$response = $xml->getResponse(); |
| 154 |
if ( empty( $response ) ) |
| 155 |
return NULL; |
| 156 |
|
| 157 |
return $response; |
| 158 |
} |
| 159 |
|
| 160 |
function create_post_by_email_address() { |
| 161 |
Jetpack::load_xml_rpc_client(); |
| 162 |
$xml = new Jetpack_IXR_Client( array( |
| 163 |
'user_id' => get_current_user_id(), |
| 164 |
) ); |
| 165 |
$xml->query( 'jetpack.createPostByEmailAddress' ); |
| 166 |
|
| 167 |
if ( $xml->isError() ) { |
| 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 |
|
| 175 |
$response = $xml->getResponse(); |
| 176 |
if ( empty( $response ) ) { |
| 177 |
echo json_encode( array( |
| 178 |
'response' => 'error', |
| 179 |
'message' => __( 'Unable to create your Post By Email address. Please try again later.', 'jetpack' ) |
| 180 |
) ); |
| 181 |
die(); |
| 182 |
} |
| 183 |
|
| 184 |
echo $response; |
| 185 |
die(); |
| 186 |
} |
| 187 |
|
| 188 |
function regenerate_post_by_email_address() { |
| 189 |
Jetpack::load_xml_rpc_client(); |
| 190 |
$xml = new Jetpack_IXR_Client( array( |
| 191 |
'user_id' => get_current_user_id(), |
| 192 |
) ); |
| 193 |
$xml->query( 'jetpack.regeneratePostByEmailAddress' ); |
| 194 |
|
| 195 |
if ( $xml->isError() ) { |
| 196 |
echo json_encode( array( |
| 197 |
'response' => 'error', |
| 198 |
'message' => __( 'Unable to regenerate your Post By Email address. Please try again later.', 'jetpack' ) |
| 199 |
) ); |
| 200 |
die(); |
| 201 |
} |
| 202 |
|
| 203 |
$response = $xml->getResponse(); |
| 204 |
if ( empty( $response ) ) { |
| 205 |
echo json_encode( array( |
| 206 |
'response' => 'error', |
| 207 |
'message' => __( 'Unable to regenerate your Post By Email address. Please try again later.', 'jetpack' ) |
| 208 |
) ); |
| 209 |
die(); |
| 210 |
} |
| 211 |
|
| 212 |
echo $response; |
| 213 |
die(); |
| 214 |
} |
| 215 |
|
| 216 |
function delete_post_by_email_address() { |
| 217 |
Jetpack::load_xml_rpc_client(); |
| 218 |
$xml = new Jetpack_IXR_Client( array( |
| 219 |
'user_id' => get_current_user_id(), |
| 220 |
) ); |
| 221 |
$xml->query( 'jetpack.deletePostByEmailAddress' ); |
| 222 |
|
| 223 |
if ( $xml->isError() ) { |
| 224 |
echo json_encode( array( |
| 225 |
'response' => 'error', |
| 226 |
'message' => __( 'Unable to disable your Post By Email address. Please try again later.', 'jetpack' ) |
| 227 |
) ); |
| 228 |
die(); |
| 229 |
} |
| 230 |
|
| 231 |
$response = $xml->getResponse(); |
| 232 |
if ( empty( $response ) ) { |
| 233 |
echo json_encode( array( |
| 234 |
'response' => 'error', |
| 235 |
'message' => __( 'Unable to disable your Post By Email address. Please try again later.', 'jetpack' ) |
| 236 |
) ); |
| 237 |
die(); |
| 238 |
} |
| 239 |
|
| 240 |
echo $response; |
| 241 |
die(); |
| 242 |
} |
| 243 |
} |
| 244 |
|