PageRenderTime 101ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-content/plugins/wp-sendgrid/includes/sendgrid-settings.php

https://github.com/mhoofman/wordpress-heroku
PHP | 256 lines | 191 code | 40 blank | 25 comment | 19 complexity | 865490b0d87666e4d8cf24b11a44d02c MD5 | raw file
  1. <?php
  2. class WP_SendGrid_Settings {
  3. // Unique identifier for the settings page
  4. const SETTINGS_PAGE_SLUG = 'wp-sendgrid-settings';
  5. const SETTINGS_SECTION_ID = 'wp-sendgrid-account-settings';
  6. // Where WP SendGrid settings are stored
  7. const SETTINGS_OPTION_NAME = 'wp_sendgrid_options';
  8. const SETTINGS_NETWORK_OPTION_NAME = 'wp_sendgrid_network_options';
  9. // Constants to hold the API access options
  10. const API_REST = 'rest';
  11. const API_SMTP = 'smtp';
  12. private static $settings;
  13. private static $default_settings = array(
  14. 'username' => '',
  15. 'password' => '',
  16. 'api' => self::API_REST,
  17. 'secure' => false
  18. );
  19. public static function start() {
  20. add_action( 'admin_init', array( __CLASS__, 'register_settings' ) );
  21. add_action( 'admin_menu', array( __CLASS__, 'register_menu' ) );
  22. add_action( 'network_admin_menu', array( __CLASS__, 'register_network_menu') );
  23. add_action( 'current_screen', array( __CLASS__, 'queue_resources' ) );
  24. add_action( 'wp_ajax_wp_sendgrid_check_settings', array( __CLASS__, 'ajax_check_settings' ) );
  25. // This action will be triggered when the user submits the network admin SG settings form.
  26. // Based on: http://wordpress.stackexchange.com/questions/64968/settings-api-in-multisite-missing-update-message
  27. add_action( 'network_admin_edit_update_sendgrid_network_options', array(__CLASS__, 'update_network_options'));
  28. }
  29. public static function register_settings() {
  30. $option_name = ( self::is_network_admin_page() ) ? self::SETTINGS_NETWORK_OPTION_NAME : self::SETTINGS_OPTION_NAME;
  31. // Register settings and sections
  32. register_setting( self::SETTINGS_PAGE_SLUG, $option_name, array( __CLASS__, 'validate_settings' ) );
  33. add_settings_section( self::SETTINGS_SECTION_ID, __( 'Account Settings' ), array( __CLASS__, 'show_settings_section_description' ), self::SETTINGS_PAGE_SLUG );
  34. // Username/Password
  35. self::add_settings_field( 'username', __( 'Your SendGrid Username' ), 'text' );
  36. self::add_settings_field( 'password', __( 'Your SendGrid Password' ), 'password' );
  37. self::add_settings_field( 'api', __( 'Send Emails With' ), 'select', array(
  38. 'description' => __( 'You shouldn\'t need to change this unless the default doesn\'t work on your server' ),
  39. 'options' => array( self::API_REST => __( 'REST' ), self::API_SMTP => __( 'SMTP' ) )
  40. ) );
  41. self::add_settings_field( 'secure', __( 'Secure Connection' ), 'checkbox', array(
  42. 'description' => ' Make sure you have the SSL extension for PHP installed before enabling.',
  43. 'label' => 'Use a secure connection (recommended).'
  44. ) );
  45. if ( self::is_network_admin_page() ) {
  46. // Only add the override option if viewing the settings under network admin
  47. self::add_settings_field( 'override', __('Allow Override'), 'checkbox', array(
  48. 'label' => __('Allow individual sites to override the network-level settings'),
  49. ));
  50. }
  51. //add_settings_field( self::SETTINGS_SECTION_ID . '-username', 'Your SendGrid Username', array( __CLASS__, 'show_settings_field' ), self::SETTINGS_PAGE_SLUG, self::SETTINGS_SECTION_ID );
  52. }
  53. public static function register_menu() {
  54. // Check to be sure the network settings allow for individual site override, before adding option page
  55. $network_settings = self::get_network_settings();
  56. if ( $network_settings['override'] ) {
  57. add_options_page( __( 'SendGrid Settings' ), __( 'SendGrid Settings' ),
  58. 'manage_options', self::SETTINGS_PAGE_SLUG, array( __CLASS__, 'show_settings_page' ) );
  59. }
  60. }
  61. public static function register_network_menu() {
  62. // add_options_page does not work for the network admin page, so we need to use add_submenu_page
  63. add_submenu_page('settings.php', __('SendGrid Settings'), __('SendGrid Settings'),
  64. 'manage_network_options', self::SETTINGS_PAGE_SLUG, array(__CLASS__, 'show_settings_page'));
  65. }
  66. public static function queue_resources($screen) {
  67. if ( 'settings_page_' . self::SETTINGS_PAGE_SLUG == $screen->base || 'settings_page_' . self::SETTINGS_PAGE_SLUG . '-network' == $screen->base ) {
  68. wp_enqueue_script( 'wp-sendgrid', WP_SendGrid::plugin_url( 'resources/wp-sendgrid.js' ), array( 'jquery' ) );
  69. wp_enqueue_style( 'wp-sendgrid', WP_SendGrid::plugin_url( 'resources/wp-sendgrid.css' ) );
  70. }
  71. }
  72. public static function ajax_check_settings() {
  73. $user_id = get_current_user_id();
  74. $user = get_userdata( $user_id );
  75. if ( wp_mail( $user->user_email, __( 'SendGrid Test' ), __( 'If you\'re reading this, it looks like your SendGrid settings are correct' ) ) ) {
  76. wp_send_json( array( 'success' => 'Test email sent. If it doesn\'t show up, double check your settings.' ) );
  77. } else {
  78. wp_send_json( array( 'error' => 'There was a problem sending the test email. Double check your settings.' ) );
  79. }
  80. }
  81. public static function show_settings_page() {
  82. if ( self::is_network_admin_page() ) {
  83. // Compose the network admin url to post to.
  84. $url = add_query_arg( 'action', 'update_sendgrid_network_options', network_admin_url('edit.php'));
  85. if ( isset( $_REQUEST['settings-updated'] ) ) {
  86. // If the request contains a 'settings-updated' parameter, we know we just saved the settings
  87. // and need to display an 'updated' message along with the 'test' button.
  88. // We can't call 'validate_settings' then 'settings_errors' because the 'update_network_options'
  89. // call happens outside of the normal sttings API (we had to create a custom method to handle
  90. // saving the network admin settings)
  91. ?>
  92. <div id='setting-error-wp_sendgrid_settings_updated' class='updated settings-error'>
  93. <p>
  94. <strong><?php _e( 'SendGrid options updated' ); ?>
  95. <input type="button" class="button" id="wp-sendgrid-test-settings" value="<?php echo esc_attr( __( 'Send Test Email' ) ); ?>" />
  96. <span class="spinner"></span>
  97. <span id="wp-sendgrid-test-settings-response"></span>
  98. </strong>
  99. </p>
  100. </div>
  101. <?php
  102. }
  103. } else {
  104. // Use the regular admin options url.
  105. $url = admin_url( 'options.php' );
  106. }
  107. ?>
  108. <div class="wrap">
  109. <h2><?php _e( 'WP SendGrid Settings' ); ?></h2>
  110. <form action="<?php echo esc_url( $url ); ?>" method="POST">
  111. <?php do_action( 'wp_sendgrid_settings_form_begin' ); ?>
  112. <?php settings_fields( self::SETTINGS_PAGE_SLUG ); ?>
  113. <?php do_settings_sections( self::SETTINGS_PAGE_SLUG ); ?>
  114. <?php do_action( 'wp_sendgrid_settings_before_submit_button'); ?>
  115. <?php submit_button(); ?>
  116. <?php do_action( 'wp_sendgrid_settings_after_submit_button' ); ?>
  117. <?php do_action( 'wp_sendgrid_settings_form_end' ); ?>
  118. </form>
  119. </div>
  120. <?php
  121. }
  122. public static function show_settings_section_description() {
  123. WP_SendGrid::load_view( 'settings-section-description.php' );
  124. }
  125. public static function add_settings_field( $id, $label, $type, $args = array() ) {
  126. $default_args = array(
  127. 'id' => $id,
  128. 'description' => '',
  129. 'type' => $type,
  130. );
  131. $args = array_merge( $default_args, $args );
  132. $callback = isset( $args['callback'] ) ? $args['callback'] : array( __CLASS__, "show_{$type}_field" );
  133. add_settings_field( self::SETTINGS_SECTION_ID . '-' . $id, $label, $callback, self::SETTINGS_PAGE_SLUG, self::SETTINGS_SECTION_ID, $args );
  134. }
  135. public static function load_field_view( $view, $args ) {
  136. $args['settings'] = self::is_network_admin_page() ? self::get_network_settings() : self::get_local_settings();
  137. $args['value'] = $args['settings'][$args['id']];
  138. WP_SendGrid::load_view( $view, $args );
  139. }
  140. public static function show_input_field( $type, $args ) {
  141. $args['type'] = $type;
  142. self::load_field_view( 'input-field.php', $args );
  143. }
  144. public static function show_text_field( $args ) {
  145. self::show_input_field( 'text', $args );
  146. }
  147. public static function show_password_field( $args ) {
  148. self::show_input_field( 'password', $args );
  149. }
  150. public static function show_select_field( $args ) {
  151. self::load_field_view( 'select-field.php', $args );
  152. }
  153. public static function show_checkbox_field( $args ) {
  154. self::load_field_view( 'checkbox-field.php', $args );
  155. }
  156. public static function get_default_settings() {
  157. return apply_filters( 'wp_sendgrid_default_settings', self::$default_settings );
  158. }
  159. public static function get_default_network_settings() {
  160. $settings = array_merge( self::$default_settings, array( 'override' => true ) );
  161. return apply_filters( 'wp_sendgrid_default_network_settings', $settings );
  162. }
  163. public static function validate_settings( $settings ) {
  164. add_settings_error( 'general', 'wp_sendgrid_settings_updated',
  165. __( 'SendGrid options updated' ) .
  166. ' <input type="button" class="button" id="wp-sendgrid-test-settings" value="' .
  167. esc_attr( __( 'Send Test Email' ) ) . '" /><span class="spinner"></span>' .
  168. ' <span id="wp-sendgrid-test-settings-response"></span>', 'updated' );
  169. $settings = apply_filters( 'wp_sendgrid_validate_settings', $settings );
  170. return $settings;
  171. }
  172. public static function get_settings() {
  173. if ( isset( self::$settings ) ) {
  174. return self::$settings;
  175. }
  176. // First check the network settings.
  177. $settings = self::get_network_settings();
  178. // If network settings are empty, or override is enabled, check local settings
  179. if ( isset( $settings['override'] ) && $settings['override'] && !self::is_network_admin_page() ) {
  180. $local_settings = self::get_local_settings();
  181. if ( !empty( $local_settings ) ) {
  182. $settings = array_merge( $settings, $local_settings );
  183. }
  184. }
  185. self::$settings = apply_filters( 'wp_sendgrid_get_settings', $settings );
  186. return self::$settings;
  187. }
  188. private static function get_local_settings() {
  189. $settings = array_merge( self::get_default_settings(), get_option( self::SETTINGS_OPTION_NAME, array() ) );
  190. return apply_filters( 'wp_sendgrid_get_local_settings', $settings );
  191. }
  192. private static function get_network_settings() {
  193. $settings = get_site_option( self::SETTINGS_NETWORK_OPTION_NAME, array());
  194. $settings = array_merge( self::get_default_network_settings(), $settings );
  195. return apply_filters( 'wp_sendgrid_get_network_settings', $settings );
  196. }
  197. private static function is_network_admin_page() {
  198. return ( defined( 'WP_NETWORK_ADMIN' ) && WP_NETWORK_ADMIN == 1 );
  199. }
  200. public static function update_network_options() {
  201. // Only make changes if the self::SETTINGS_OPTION_NAME key exists.
  202. // Note: we use the SETTINGS_OPTION_NAME because it is used by files in the '/views' folder
  203. if ( isset( $_REQUEST[self::SETTINGS_OPTION_NAME] ) ) {
  204. $value = stripslashes_deep( $_REQUEST[self::SETTINGS_OPTION_NAME] );
  205. // Since a false value from the override checkbox won't be saved, we need to add it here.
  206. $_REQUEST[self::SETTINGS_OPTION_NAME]['override'] = isset( $_REQUEST[self::SETTINGS_OPTION_NAME]['override'] );
  207. // Update the network option.
  208. update_site_option( self::SETTINGS_NETWORK_OPTION_NAME, $_REQUEST[self::SETTINGS_OPTION_NAME] );
  209. // Redirect back to the network settings page.
  210. $params = array( 'page' => self::SETTINGS_PAGE_SLUG, 'settings-updated' => 'true' );
  211. wp_redirect( add_query_arg( $params, network_admin_url('settings.php') ) );
  212. exit();
  213. }
  214. }
  215. }
  216. WP_SendGrid_Settings::start();