/modules/after-the-deadline.php

  1. <?php 
  2. /** 
  3. * Module Name: Spelling and Grammar 
  4. * Module Description: Check your spelling, style, and grammar with the After the Deadline proofreading service. 
  5. * Sort Order: 6 
  6. * First Introduced: 1.1 
  7. * Requires Connection: Yes 
  8. * Auto Activate: Yes 
  9. * Module Tags: Writing 
  10. * Additional Search Queries: after the deadline, afterthedeadline, spell, spellchecker, spelling, grammar, proofreading, style, language, cliche 
  11. */ 
  12.  
  13. if ( defined( 'IS_WPCOM' ) && IS_WPCOM ) { 
  14. // This wpcom-specific code should eventually be moved elsewhere. 
  15.  
  16. function AtD_http_post_timeout_action() { 
  17. return 5; 
  18. add_filter( 'atd_http_post_timeout', 'AtD_http_post_timeout_action' ); 
  19. function AtD_http_post_error_action( $code ) { 
  20. /** This action is documented in modules/widgets/social-media-icons.php */ 
  21. do_action( 'jetpack_bump_stats_extras', 'atd-remote-error', $code ); 
  22. add_action( 'atd_http_post_error', 'AtD_http_post_error_action' ); 
  23. function AtD_service_domain_action() { 
  24. return 'en.service.afterthedeadline.com'; 
  25. add_filter( 'atd_service_domain', 'AtD_service_domain_action' ); 
  26. function AtD_update_setting( $user_id, $name, $value ) { 
  27. update_user_attribute( $user_id, $name, $value ); 
  28. function AtD_get_setting( $user_id, $name, $single = true ) { 
  29. return get_user_attribute( $user_id, $name ); 
  30. function AtD_get_rpc_id() { 
  31. return get_bloginfo( 'wpurl' ); 
  32. } else { 
  33. // This code is used only in Jetpack. 
  34.  
  35. add_action( 'jetpack_modules_loaded', 'AtD_load' ); 
  36. function AtD_load() { 
  37. Jetpack::enable_module_configurable( __FILE__ ); 
  38. Jetpack::module_configuration_load( __FILE__, 'AtD_configuration_load' ); 
  39. function AtD_configuration_load() { 
  40. wp_safe_redirect( get_edit_profile_url( get_current_user_id() ) . '#atd' ); 
  41. exit; 
  42. function AtD_update_setting( $user_id, $name, $value ) { 
  43. update_user_meta( $user_id, $name, $value ); 
  44. function AtD_get_setting( $user_id, $name, $single = true ) { 
  45. return get_user_meta( $user_id, $name, $single ); 
  46. function AtD_get_rpc_id() { 
  47. return 'WPORG-' . md5( get_bloginfo( 'wpurl ') ); 
  48.  
  49. /** 
  50. * Load necessary include files 
  51. */ 
  52. include( dirname( __FILE__ ) . '/after-the-deadline/config-options.php' ); 
  53. include( dirname( __FILE__ ) . '/after-the-deadline/config-unignore.php' ); 
  54. include( dirname( __FILE__ ) . '/after-the-deadline/proxy.php' ); 
  55.  
  56. define( 'ATD_VERSION', '20150715' ); 
  57.  
  58. /** 
  59. * Display the AtD configuration options 
  60. */ 
  61. function AtD_config() { 
  62. AtD_display_options_form(); 
  63. AtD_display_unignore_form(); 
  64.  
  65. /** 
  66. * Code to update the toolbar with the AtD Button and Install the AtD TinyMCE Plugin 
  67. */ 
  68. function AtD_addbuttons() { 
  69. /** Don't bother doing this stuff if the current user lacks permissions */ 
  70. if ( ! AtD_is_allowed() ) 
  71. return; 
  72.  
  73. if ( ! defined( 'ATD_TINYMCE_4' ) ) { 
  74. define( 'ATD_TINYMCE_4', ( ! empty( $GLOBALS['tinymce_version'] ) && substr( $GLOBALS['tinymce_version'], 0, 1 ) >= 4 ) ); 
  75.  
  76. /** Add only in Rich Editor mode */ 
  77. if ( get_user_option( 'rich_editing' ) == 'true' ) { 
  78. add_filter( 'mce_external_plugins', 'add_AtD_tinymce_plugin' ); 
  79. add_filter( 'mce_buttons', 'register_AtD_button' ); 
  80.  
  81. add_action( 'personal_options_update', 'AtD_process_options_update' ); 
  82. add_action( 'personal_options_update', 'AtD_process_unignore_update' ); 
  83. add_action( 'profile_personal_options', 'AtD_config' ); 
  84.  
  85. /** 
  86. * Hook into the TinyMCE buttons and replace the current spellchecker 
  87. */ 
  88. function register_AtD_button( $buttons ) { 
  89. if ( ATD_TINYMCE_4 ) { 
  90. // Use the default icon in TinyMCE 4.0 (replaced by dashicons in editor.css) 
  91. if ( ! in_array( 'spellchecker', $buttons, true ) ) { 
  92. $buttons[] = 'spellchecker'; 
  93.  
  94. return $buttons; 
  95.  
  96. /** kill the spellchecker.. don't need no steenkin PHP spell checker */ 
  97. foreach ( $buttons as $key => $button ) { 
  98. if ( $button == 'spellchecker' ) { 
  99. $buttons[$key] = 'AtD'; 
  100. return $buttons; 
  101.  
  102. /** hrm... ok add us last plz */ 
  103. array_push( $buttons, '|', 'AtD' ); 
  104. return $buttons; 
  105.  
  106. /** 
  107. * Load the TinyMCE plugin : editor_plugin.js (TinyMCE 3.x) | plugin.js (TinyMCE 4.0) 
  108. */ 
  109. function add_AtD_tinymce_plugin( $plugin_array ) { 
  110. $plugin = ATD_TINYMCE_4 ? 'plugin' : 'editor_plugin'; 
  111.  
  112. $plugin_array['AtD'] = plugins_url( 'after-the-deadline/tinymce/' . $plugin . '.js?v=' . ATD_VERSION, __FILE__ ); 
  113. return $plugin_array; 
  114.  
  115. /** 
  116. * Update the TinyMCE init block with AtD specific settings 
  117. */ 
  118. function AtD_change_mce_settings( $init_array ) { 
  119. if ( ! AtD_is_allowed() ) 
  120. return $init_array; 
  121.  
  122. if ( ! is_array( $init_array ) ) 
  123. $init_array = array(); 
  124.  
  125. $user = wp_get_current_user(); 
  126.  
  127. $init_array['atd_rpc_url'] = admin_url( 'admin-ajax.php?action=proxy_atd&_wpnonce=' . wp_create_nonce( 'proxy_atd' ) . '&url=' ); 
  128. $init_array['atd_ignore_rpc_url'] = admin_url( 'admin-ajax.php?action=atd_ignore&_wpnonce=' . wp_create_nonce( 'atd_ignore' ) . '&phrase=' ); 
  129. $init_array['atd_rpc_id'] = AtD_get_rpc_id(); 
  130. $init_array['atd_theme'] = 'wordpress'; 
  131. $init_array['atd_ignore_enable'] = 'true'; 
  132. $init_array['atd_strip_on_get'] = 'true'; 
  133. $init_array['atd_ignore_strings'] = json_encode( explode( ', ', AtD_get_setting( $user->ID, 'AtD_ignored_phrases' ) ) ); 
  134. $init_array['atd_show_types'] = AtD_get_setting( $user->ID, 'AtD_options' ); 
  135. $init_array['gecko_spellcheck'] = 'false'; 
  136.  
  137. return $init_array; 
  138.  
  139. /** 
  140. * Sanitizes AtD AJAX data to acceptable chars, caller needs to make sure ' is escaped 
  141. */ 
  142. function AtD_sanitize( $untrusted ) { 
  143. return preg_replace( '/[^a-zA-Z0-9\-\', _ ]/i', "", $untrusted ); 
  144.  
  145. /** 
  146. * AtD HTML Editor Stuff 
  147. */ 
  148. function AtD_settings() { 
  149. $user = wp_get_current_user(); 
  150.  
  151. header( 'Content-Type: text/javascript' ); 
  152.  
  153. /** set the RPC URL for AtD */ 
  154. echo "AtD.rpc = " . json_encode( esc_url_raw( admin_url( 'admin-ajax.php?action=proxy_atd&_wpnonce=' . wp_create_nonce( 'proxy_atd' ) . '&url=' ) ) ) . ";\n"; 
  155.  
  156. /** set the API key for AtD */ 
  157. echo "AtD.api_key = " . json_encode( AtD_get_rpc_id() ) . ";\n"; 
  158.  
  159. /** set the ignored phrases for AtD */ 
  160. echo "AtD.setIgnoreStrings(" . json_encode( AtD_get_setting( $user->ID, 'AtD_ignored_phrases' ) ) . ");\n"; 
  161.  
  162. /** honor the types we want to show */ 
  163. echo "AtD.showTypes(" . json_encode( AtD_get_setting( $user->ID, 'AtD_options' ) ) .");\n"; 
  164.  
  165. /** this is not an AtD/jQuery setting but I'm putting it in AtD to make it easy for the non-viz plugin to find it */ 
  166. $admin_ajax_url = admin_url( 'admin-ajax.php?action=atd_ignore&_wpnonce=' . wp_create_nonce( 'atd_ignore' ) . '&phrase=' ); 
  167. echo "AtD.rpc_ignore = " . json_encode( esc_url_raw( $admin_ajax_url ) ) . ";\n"; 
  168.  
  169. die; 
  170.  
  171. function AtD_load_javascripts() { 
  172. if ( AtD_should_load_on_page() ) { 
  173. wp_enqueue_script( 'AtD_core', plugins_url( '/after-the-deadline/atd.core.js', __FILE__ ), array(), ATD_VERSION ); 
  174. wp_enqueue_script( 'AtD_quicktags', plugins_url( '/after-the-deadline/atd-nonvis-editor-plugin.js', __FILE__ ), array('quicktags'), ATD_VERSION ); 
  175. wp_enqueue_script( 'AtD_jquery', plugins_url( '/after-the-deadline/jquery.atd.js', __FILE__ ), array('jquery'), ATD_VERSION ); 
  176. wp_enqueue_script( 'AtD_settings', admin_url() . 'admin-ajax.php?action=atd_settings', array('AtD_jquery'), ATD_VERSION ); 
  177. wp_enqueue_script( 'AtD_autoproofread', plugins_url( '/after-the-deadline/atd-autoproofread.js', __FILE__ ), array('AtD_jquery'), ATD_VERSION ); 
  178.  
  179. /** load localized strings for AtD */ 
  180. wp_localize_script( 'AtD_core', 'AtD_l10n_r0ar', array ( 
  181. 'menu_title_spelling' => __( 'Spelling', 'jetpack' ),  
  182. 'menu_title_repeated_word' => __( 'Repeated Word', 'jetpack' ),  
  183.  
  184. 'menu_title_no_suggestions' => __( 'No suggestions', 'jetpack' ),  
  185.  
  186. 'menu_option_explain' => __( 'Explain...', 'jetpack' ),  
  187. 'menu_option_ignore_once' => __( 'Ignore suggestion', 'jetpack' ),  
  188. 'menu_option_ignore_always' => __( 'Ignore always', 'jetpack' ),  
  189. 'menu_option_ignore_all' => __( 'Ignore all', 'jetpack' ),  
  190.  
  191. 'menu_option_edit_selection' => __( 'Edit Selection...', 'jetpack' ),  
  192.  
  193. 'button_proofread' => __( 'proofread', 'jetpack' ),  
  194. 'button_edit_text' => __( 'edit text', 'jetpack' ),  
  195. 'button_proofread_tooltip' => __( 'Proofread Writing', 'jetpack' ),  
  196.  
  197. 'message_no_errors_found' => __( 'No writing errors were found.', 'jetpack' ),  
  198. 'message_server_error' => __( 'There was a problem communicating with the Proofreading service. Try again in one minute.', 'jetpack' ),  
  199. 'message_server_error_short' => __( 'There was an error communicating with the proofreading service.', 'jetpack' ),  
  200.  
  201. 'dialog_replace_selection' => __( 'Replace selection with:', 'jetpack' ),  
  202. 'dialog_confirm_post_publish' => __( "The proofreader has suggestions for this post. Are you sure you want to publish it?\n\nPress OK to publish your post, or Cancel to view the suggestions and edit your post.", 'jetpack' ),  
  203. 'dialog_confirm_post_update' => __( "The proofreader has suggestions for this post. Are you sure you want to update it?\n\nPress OK to update your post, or Cancel to view the suggestions and edit your post.", 'jetpack' ),  
  204. ) ); 
  205.  
  206. /** Spits out user options for auto-proofreading on publish/update */ 
  207. function AtD_load_submit_check_javascripts() { 
  208. global $pagenow; 
  209.  
  210. $user = wp_get_current_user(); 
  211. if ( ! $user || $user->ID == 0 ) 
  212. return; 
  213.  
  214. if ( AtD_should_load_on_page() ) { 
  215. $atd_check_when = AtD_get_setting( $user->ID, 'AtD_check_when' ); 
  216.  
  217. if ( !empty( $atd_check_when ) ) { 
  218. $check_when = array(); 
  219. /** Set up the options in json */ 
  220. foreach( explode( ', ', $atd_check_when ) as $option ) { 
  221. $check_when[$option] = true; 
  222. echo '<script type="text/javascript">' . "\n"; 
  223. echo 'AtD_check_when = ' . json_encode( (object) $check_when ) . ";\n"; 
  224. echo '</script>' . "\n"; 
  225.  
  226. /** 
  227. * Check if a user is allowed to use AtD 
  228. */ 
  229. function AtD_is_allowed() { 
  230. if ( ( defined( 'AtD_FORCED_ON' ) && AtD_FORCED_ON ) ) { 
  231. return true; 
  232. $user = wp_get_current_user(); 
  233. if ( ! $user || $user->ID == 0 ) 
  234. return; 
  235.  
  236. if ( ! current_user_can( 'edit_posts' ) && ! current_user_can( 'edit_pages' ) ) 
  237. return; 
  238.  
  239. return 1; 
  240.  
  241. function AtD_load_css() { 
  242. if ( AtD_should_load_on_page() ) { 
  243. if( is_rtl() ) { 
  244. wp_enqueue_style( 'AtD_style', plugins_url( '/after-the-deadline/rtl/atd-rtl.css', __FILE__ ), null, ATD_VERSION, 'screen' ); 
  245. } else { 
  246. wp_enqueue_style( 'AtD_style', plugins_url( '/after-the-deadline/atd.css', __FILE__ ), null, ATD_VERSION, 'screen' ); 
  247.  
  248. /** Helper used to check if javascript should be added to page. Helps avoid bloat in admin */ 
  249. function AtD_should_load_on_page() { 
  250. global $pagenow, $current_screen; 
  251.  
  252. $pages = array( 'post.php', 'post-new.php', 'page.php', 'page-new.php', 'admin.php', 'profile.php' ); 
  253.  
  254. if ( in_array( $pagenow, $pages ) ) { 
  255. if ( isset( $current_screen->post_type ) && $current_screen->post_type ) { 
  256. return post_type_supports( $current_screen->post_type, 'editor' ); 
  257. return true; 
  258.  
  259. /** 
  260. * Allows scripts to be loaded via AtD in admin. 
  261. * 
  262. * By default, AtD only enqueues JS on certain admin pages to reduce bloat. The filter allows additional pages to have AtD JS. 
  263. * 
  264. * @module after-the-deadline 
  265. * 
  266. * @since 1.2.3 
  267. * 
  268. * @param bool false Boolean to load or not load AtD scripts in admin. 
  269. */ 
  270. return apply_filters( 'atd_load_scripts', false ); 
  271.  
  272. // add button to DFW 
  273. if ( ! ( defined( 'IS_WPCOM' ) && IS_WPCOM ) ) { 
  274. add_filter( 'wp_fullscreen_buttons', 'AtD_fullscreen' ); 
  275. function AtD_fullscreen($buttons) { 
  276. $buttons['spellchecker'] = array( 'title' => __( 'Proofread Writing', 'jetpack' ), 'onclick' => "tinyMCE.execCommand('mceWritingImprovementTool');", 'both' => false ); 
  277. return $buttons; 
  278.  
  279. /** add some vars into the AtD plugin */ 
  280. add_filter( 'tiny_mce_before_init', 'AtD_change_mce_settings' ); 
  281.  
  282. /** load some stuff for non-visual editor */ 
  283. add_action( 'admin_enqueue_scripts', 'AtD_load_javascripts' ); 
  284. add_action( 'admin_enqueue_scripts', 'AtD_load_submit_check_javascripts' ); 
  285. add_action( 'admin_enqueue_scripts', 'AtD_load_css' ); 
  286.  
  287. /** init process for button control */ 
  288. add_action( 'init', 'AtD_addbuttons' ); 
  289.  
  290. /** setup hooks for our PHP functions we want to make available via an AJAX call */ 
  291. add_action( 'wp_ajax_proxy_atd', 'AtD_redirect_call' ); 
  292. add_action( 'wp_ajax_atd_ignore', 'AtD_ignore_call' ); 
  293. add_action( 'wp_ajax_atd_settings', 'AtD_settings' ); 
.