Advertisement
Guest User

Untitled

a guest
Jun 19th, 2019
160
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.63 KB | None | 0 0
  1. class login_account_logout_settings {
  2.  
  3. private $options;
  4. private $settings_page_name;
  5. private $settings_menu_name;
  6.  
  7. public function __construct() {
  8.  
  9. $this->settings_page_name = 'login-account-logout';
  10. $this->settings_menu_name = 'Login Account Logout';
  11.  
  12. // Initialize and register settings.
  13. add_action( 'admin_init', array( $this, 'register_settings' ) );
  14. // Add settings page.
  15. add_action( 'admin_menu', array( $this, 'add_settings_page' ) );
  16. // Add settings link to plugins page.
  17. add_action( 'plugin_action_links_' . plugin_basename( __FILE__ ), array( $this, 'add_settings_link' ) );
  18. }
  19.  
  20. public function register_settings() {
  21.  
  22. register_setting( 'lal_settingz', 'lal_settingz', array( $this, 'sanitize' ) );
  23.  
  24. // ID / title / callback / page
  25. add_settings_section( 'configuration_section', __( 'Configuration', 'login-account-logout' ), array( $this, 'print_section_info' ), $this->settings_page_name );
  26.  
  27. // ID / title / callback / page / section
  28. add_settings_field( 'lal_display_type', __( 'Display type', 'login-account-logout' ), array( $this, 'lal_display_type_callback' ), $this->settings_page_name, 'configuration_section' );
  29. add_settings_field( 'lal_login_text', __( 'Login link text', 'login-account-logout' ), array( $this, 'lal_login_text_callback' ), $this->settings_page_name, 'configuration_section' );
  30. add_settings_field( 'lal_logout_text', __( 'Logout link text', 'login-account-logout' ), array( $this, 'lal_logout_text_callback' ), $this->settings_page_name, 'configuration_section' );
  31. add_settings_field( 'lal_account_text', __( 'Account link text', 'login-account-logout' ), array( $this, 'lal_account_text_callback' ), $this->settings_page_name, 'configuration_section' );
  32. add_settings_field( 'lal_position', __( 'Position', 'login-account-logout' ), array( $this, 'lal_position_callback' ), $this->settings_page_name, 'configuration_section' );
  33. add_settings_field( 'lal_container', __( 'Container selector', 'login-account-logout' ), array( $this, 'lal_container_callback' ), $this->settings_page_name, 'configuration_section' );
  34. }
  35.  
  36. public function add_settings_page() {
  37. // This page will be under "Settings"
  38. add_options_page(
  39. 'Settings Admin', $this->settings_menu_name, 'manage_options', $this->settings_page_name, array( $this, 'create_settings_page' )
  40. );
  41. }
  42.  
  43. /**
  44. * Get the option that is saved or the default.
  45. *
  46. * @param string $index. The option we want to get.
  47. */
  48. public function lal_get_settings( $index = false ) {
  49.  
  50. $defaults = array ( 'lal_display_type' => 'icon', 'lal_login_text' => 'Log in', 'lal_logout_text' => 'Log out',
  51. 'lal_account_text' => 'My account', 'lal_position' => 'after', 'lal_container' => 'nav.secondary-navigation' );
  52. $settings = get_option( 'lal_settingz', $defaults );
  53.  
  54. if ( $index && isset( $settings[ $index ] ) ) {
  55. return $settings[ $index ];
  56. }
  57.  
  58. return $settings;
  59. }
  60.  
  61. public function create_settings_page() {
  62.  
  63. $this->options = $this->lal_get_settings();
  64.  
  65. ?>
  66. <div class="wrap">
  67. <h1>Login Account Logout</h1>
  68. <form method="post" action="options.php">
  69. <?php
  70. // This prints out all hidden setting fields
  71. settings_fields( 'lal_settingz' );
  72. do_settings_sections( $this->settings_page_name );
  73. submit_button();
  74. ?>
  75. </form>
  76. </div>
  77. <?php
  78. }
  79.  
  80. public function add_settings_link( $links ) {
  81. $links = array_merge( array(
  82. '<a href="' . esc_url( admin_url( '/options-general.php?page=' . $this->settings_page_name ) ) . '">' . __( 'Settings' ) . '</a>'
  83. ), $links );
  84. return $links;
  85. }
  86.  
  87. /**
  88. * Sanitize each setting field as needed
  89. *
  90. * @param array $input Contains all settings fields as array keys
  91. */
  92. public function sanitize( $input ) {
  93. $new_input = array();
  94. if( isset( $input['lal_display_type'] ) )
  95. $new_input['lal_display_type'] = sanitize_text_field( $input['lal_display_type'] );
  96. if( isset( $input['lal_login_text'] ) )
  97. $new_input['lal_login_text'] = sanitize_text_field( $input['lal_login_text'] );
  98. if( isset( $input['lal_logout_text'] ) )
  99. $new_input['lal_logout_text'] = sanitize_text_field( $input['lal_logout_text'] );
  100. if( isset( $input['lal_account_text'] ) )
  101. $new_input['lal_account_text'] = sanitize_text_field( $input['lal_account_text'] );
  102. if( isset( $input['lal_position'] ) )
  103. $new_input['lal_position'] = sanitize_text_field( $input['lal_position'] );
  104. if( isset( $input['lal_container'] ) )
  105. $new_input['lal_container'] = sanitize_text_field( $input['lal_container'] );
  106. return $new_input;
  107. }
  108.  
  109. /**
  110. * Print the Section text
  111. */
  112.  
  113. public function print_section_info() {
  114. return;
  115. // print 'Enter your settings below:';
  116. }
  117.  
  118. /**
  119. * Get the settings option array and print one of its values
  120. */
  121.  
  122. public function lal_display_type_callback() {
  123.  
  124. $items = array ('icon', 'links');
  125.  
  126. echo '<select id="lal_display_type" name="lal_settingz[lal_display_type]">';
  127. foreach($items as &$item) {
  128.  
  129. $selected = (isset( $this->options['lal_display_type'])) && ( $item == $this->options['lal_display_type']) ? 'selected="selected"' : '';
  130. $item = __( $item, 'login-account-logout' );
  131. printf(
  132. '<option value="%1$s" %2$s>%1$s</option>',
  133. esc_attr($item),
  134. $selected
  135. );
  136. }
  137. printf(
  138. '</select>
  139. <p class="description">%1$s</p>',
  140. __( 'Choose between an icon directing to My account page and dynamic login / account / logout links. Icon allows to save screen space and appears immediately even for slow websites. Links offer direct access to logout, but take more space and show as soon as website scripts execute.', 'login-account-logout' )
  141. );
  142. }
  143.  
  144. public function lal_login_text_callback() {
  145. printf(
  146. '<input type="text" id="lal_login_text" name="lal_settingz[lal_login_text]" value="%1$s" />',
  147. isset( $this->options['lal_login_text'] ) ? esc_attr( $this->options['lal_login_text']) : ''
  148. );
  149. }
  150.  
  151. public function lal_logout_text_callback() {
  152. printf(
  153. '<input type="text" id="lal_logout_text" name="lal_settingz[lal_logout_text]" value="%1$s" />',
  154. isset( $this->options['lal_logout_text'] ) ? esc_attr( $this->options['lal_logout_text']) : ''
  155. );
  156. }
  157.  
  158. public function lal_account_text_callback() {
  159. printf(
  160. '<input type="text" id="lal_account_text" name="lal_settingz[lal_account_text]" value="%1$s" />',
  161. isset( $this->options['lal_account_text'] ) ? esc_attr( $this->options['lal_account_text']) : ''
  162. );
  163. }
  164.  
  165. public function lal_position_callback() {
  166.  
  167. $items = array ('before', 'after');
  168.  
  169. echo '<select id="lal_position" name="lal_settingz[lal_position]">';
  170. foreach($items as &$item) {
  171.  
  172. $selected = (isset( $this->options['lal_position'])) && ( $item == $this->options['lal_position']) ? 'selected="selected"' : '';
  173. $item = __( $item, 'login-account-logout' );
  174. printf(
  175. '<option value="%1$s" %2$s>%1$s</option>',
  176. esc_attr($item),
  177. $selected
  178. );
  179. }
  180. printf(
  181. '</select>
  182. <p class="description">%1$s</p>',
  183. __( 'Choose whether to display icon / links before other elements in container (for example shopping cart) or after them. If container is empty, this setting won't make a difference.', 'login-account-logout' )
  184. );
  185. }
  186.  
  187. public function lal_container_callback() {
  188. printf(
  189. '<input type="text" id="lal_container" name="lal_settingz[lal_container]" value="%1$s" />
  190. <p class="description">%2$s</p>',
  191. isset( $this->options['lal_container'] ) ? esc_attr( $this->options['lal_container']) : '',
  192. __( 'jQuery selector to find an element serving as container for icon / links. All popular and most of less common CSS selectors are valid jQuery selectors.', 'login-account-logout' )
  193. );
  194. }
  195. }
  196.  
  197. if( is_admin() ) {
  198. $lal_settingz_page = new login_account_logout_settings();
  199. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement