PageRenderTime 26ms CodeModel.GetById 30ms RepoModel.GetById 0ms app.codeStats 1ms

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

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