PageRenderTime 23ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-content/plugins/jetpack/modules/after-the-deadline.php

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