From e164efcd3ad883ec41dc17c74ebc0510b22af5e9 Mon Sep 17 00:00:00 2001
From: Stefan Kalscheuer <stefan@stklcode.de>
Date: Sun, 12 Nov 2017 14:15:20 +0100
Subject: [PATCH] Settings page moved into admin class

---
 includes/admin/page-settings.php       | 151 -------------------------
 includes/class-wpliveticker2-admin.php | 143 ++++++++++++++++++++++-
 includes/class-wpliveticker2.php       |   4 +
 3 files changed, 145 insertions(+), 153 deletions(-)
 delete mode 100644 includes/admin/page-settings.php

diff --git a/includes/admin/page-settings.php b/includes/admin/page-settings.php
deleted file mode 100644
index 8608e4d..0000000
--- a/includes/admin/page-settings.php
+++ /dev/null
@@ -1,151 +0,0 @@
-<?php
-/**
- * @package Settings Page
- */
-
-// Exit if accessed directly
-if ( !defined( 'ABSPATH' ) ) exit;
-
-/**
- * Register settings page
- *
- * @return void
- */
-function wplt_register_page_settings() {
-	add_submenu_page( 'edit.php?post_type=wplt_tick', 'Liveticker2 ' . __( 'Settings', 'wplt2' ), __( 'Settings', 'wplt2' ), 'manage_options', 'wplt_settings', 'wplt_render_page_settings' );
-}
-add_action( 'admin_menu', 'wplt_register_page_settings' );
-
-/**
- * Register settings API
- *
- * @return void
- */
-function wplt_register_settings() {
-	register_setting( 'wplt_settings', 'wplt2', 'wplt_validate_settings' ); 
-	
-	// Form sections
-	add_settings_section( 'wplt_settings_general', __( 'General', 'wplt2' ), 'wplt_settings_general_section', __FILE__ );
-	add_settings_section( 'wplt_settings_uninstall', __( 'Uninstall', 'wplt2' ), 'wplt_settings_uninstall_section', __FILE__ );
-	
-	// Form fields
-	add_settings_field( 'enable_css', __( 'Default CSS Styles', 'wplt2' ), 'wplt_settings_enable_css_field', __FILE__, 'wplt_settings_general' );
-	add_settings_field( 'reset_settings', __( 'Reset Settings', 'wplt2' ), 'wplt_settings_reset_settings_field', __FILE__, 'wplt_settings_uninstall' );
-} 
-add_action( 'admin_init', 'wplt_register_settings' );
-
-/**
- * Validate settings callback
- *
- * @return void
- */
-function wplt_validate_settings( $input ) {
-	 $defaults = wplt_get_default_options();
-	 
-	 $parsed = wp_parse_args( $input, $defaults );
-	 
-	 // Fix empty default text textbox
-	 if( trim( $input['default_text'] == '' ) ) {
-		 $parsed['default_text'] = $defaults['default_text'];
-	 }
-	 
-	 return $parsed;
-}
-
-/**
- * Render settings page
- *
- * @return void
- */
-function wplt_render_page_settings() {
-	?>
-	<div class="wrap">
-		<div id="icon-options-general" class="icon32"><br></div>
-		<h2>Liveticker <?php _e( 'Settings', 'wplt2' ); ?></h2>
-		<?php if ( isset( $_GET['settings-updated'] ) ) {
-			echo '<div class="updated"><p>' . __( 'Settings updated successfully.', 'wplt2' ) . '</p></div>';
-		} ?>
-		<form action="options.php" method="post">
-			<?php 
-				settings_fields( 'wplt_settings' ); 
-				do_settings_sections( __FILE__ );
-			?>
-			<p class="submit">
-				<input type="submit" name="submit" id="submit" class="button button-primary" value="Save Changes">
-			</p>
-		</form>
-	</div>
-	<?php
-}
-
-/**
- * Render general section
- *
- * @return void
- */
-function wplt_settings_general_section() {
-	return;
-}
-
-/**
- * Render uninstall section
- *
- * @return void
- */
-function wplt_settings_uninstall_section() {
-	return;
-}
-
-/**
- * Render enable css field
- *
- * @return void
- */
-function wplt_settings_enable_css_field() {
-	global $wplt_options;
-	
-	$checked = $wplt_options['enable_css'];
-
-	echo '<label for="wp-liveticker2[enable_css]">';
-	echo '<input type="checkbox" name="wp-liveticker2[enable_css]" value="1" ' . checked( $checked, 1, false ) . ' /> ';
-	echo __( 'Enable', 'wplt2' );
-	echo '</label>';
-	echo '<p class="description">' . __( 'Disable this option to remove the default button styling and the Delightful Downloads CSS file.', 'wplt2' ) . '</p>';
-}
-
-/**
- * Render default style field
- *
- * @return void
- */
-function wplt_settings_default_style_field() {
-	global $wplt_options;
-
-	$styles = wplt_get_shortcode_styles();
-	$default_style = $wplt_options['default_style'];
-
-	echo '<select name="simple-downloads[default_style]">';
-	foreach( $styles as $key => $value ) {
-		$selected = ( $default_style == $key ? ' selected="selected"' : '' );
-		echo '<option value="' . $key . '" ' . $selected . '>' . $value . '</option>';	
-	}
-	echo '</select>';
-	echo '<p class="description">' . __( 'The default display style.', 'wplt2' ) . '</p>';
-}
-
-/**
- * Render reset settings field
- *
- * @return void
- */
-function wplt_settings_reset_settings_field() {
-	global $wplt_options;
-	
-	$checked = $wplt_options['reset_settings'];
-	
-	echo '<label for="simple-downloads[reset_settings]">';
-	echo '<input type="checkbox" name="simple-downloads[reset_settings]" value="1" ' . checked( $checked, 1, false ) . ' /> ';
-	echo __( 'Enable', 'wplt2' );
-	echo '<p class="description">' . __( 'Reset plugin settings on re-activation.', 'wplt2' ) . '</p>';
-	echo '</label>';
-}
\ No newline at end of file
diff --git a/includes/class-wpliveticker2-admin.php b/includes/class-wpliveticker2-admin.php
index aad9b88..6527220 100644
--- a/includes/class-wpliveticker2-admin.php
+++ b/includes/class-wpliveticker2-admin.php
@@ -25,8 +25,147 @@ class WPLiveticker2_Admin extends WPLiveticker2 {
 		$total_files = wp_count_posts( 'wplt2_tick' );
 
 		echo '<tr>';
-		echo '<td class="first b b-tags"><a href="edit.php?post_type=wplt_tick">' . esc_html( $total_files->publish ) . '</a></td>';
-		echo '<td class="t tags"><a href="edit.php?post_type=wplt_tick">' . esc_html__( 'Ticks', 'wplt2' ) . '</a></td>';
+		echo '<td class="first b b-tags"><a href="edit.php?post_type=wplt2_tick">' . esc_html( $total_files->publish ) . '</a></td>';
+		echo '<td class="t tags"><a href="edit.php?post_type=wplt2_tick">' . esc_html__( 'Ticks', 'wplt2' ) . '</a></td>';
 		echo '</tr>';
 	}
+
+	/**
+	 * Register settings page.
+	 *
+	 * @return void
+	 */
+	public static function register_settings_page() {
+		add_submenu_page(
+			'edit.php?post_type=wplt2_tick',
+			'Liveticker2 ' . __( 'Settings', 'wplt2' ),
+			__( 'Settings', 'wplt2' ),
+			'manage_options',
+			'wplt2_settings',
+			array(
+				'WPLiveticker2_Admin',
+				'settings_page',
+			)
+		);
+	}
+
+	/**
+	 * Register settings API
+	 *
+	 * @return void
+	 */
+	public static function register_settings() {
+		register_setting( 'wplt2_settings', 'wplt2', array( 'WPLiveticker2_Admin', 'validate_settings' ) );
+
+		// Form sections.
+		add_settings_section( 'wplt2_settings_general', __( 'General', 'wplt2' ), array( 'WPLiveticker2_Admin', 'settings_general_section' ), __FILE__ );
+		add_settings_section( 'wplt2_settings_uninstall', __( 'Uninstall', 'wplt2' ), array( 'WPLiveticker2_Admin', 'settings_uninstall_section' ), __FILE__ );
+
+		// Form fields.
+		add_settings_field( 'enable_css', __( 'Default CSS Styles', 'wplt2' ), array( 'WPLiveticker2_Admin', 'settings_enable_css_field' ), __FILE__, 'wplt2_settings_general' );
+		add_settings_field( 'reset_settings', __( 'Reset Settings', 'wplt2' ), array( 'WPLiveticker2_Admin', 'settings_reset_settings_field' ), __FILE__, 'wplt2_settings_uninstall' );
+	}
+
+	/**
+	 * Render general section.
+	 *
+	 * @return void
+	 */
+	public static function settings_general_section() {
+	}
+
+	/**
+	 * Render uninstall section.
+	 *
+	 * @return void
+	 */
+	public static function settings_uninstall_section() {
+	}
+
+	/**
+	 * Render enable css field.
+	 *
+	 * @return void
+	 */
+	public static function settings_enable_css_field() {
+		$checked = self::$_options['enable_css'];
+
+		echo '<label for="wp-liveticker2[enable_css]">';
+		echo '<input type="checkbox" name="wp-liveticker2[enable_css]" value="1" ' . checked( $checked, 1, false ) . ' /> ';
+		esc_html_e( 'Enable', 'wplt2' );
+		echo '</label>';
+		echo '<p class="description">' . esc_html__( 'Disable this option to remove the default button styling and the Delightful Downloads CSS file.', 'wplt2' ) . '</p>';
+	}
+
+	/**
+	 * Render default style field.
+	 *
+	 * @return void
+	 */
+	public static function settings_default_style_field() {
+		$styles        = wplt_get_shortcode_styles();
+		$default_style = self::$_options['default_style'];
+
+		echo '<select name="simple-downloads[default_style]">';
+		foreach ( $styles as $key => $value ) {
+			$selected = ( $default_style === $key ? ' selected="selected"' : '' );
+			echo '<option value="' . esc_attr( $key ) . '" ' . esc_attr( $selected ) . '>' . esc_html( $value ) . '</option>';
+		}
+		echo '</select>';
+		echo '<p class="description">' . esc_html__( 'The default display style.', 'wplt2' ) . '</p>';
+	}
+
+	/**
+	 * Render reset settings field
+	 *
+	 * @return void
+	 */
+	public static function settings_reset_settings_field() {
+		$checked = self::$_options['reset_settings'];
+
+		echo '<label for="simple-downloads[reset_settings]">';
+		echo '<input type="checkbox" name="simple-downloads[reset_settings]" value="1" ' . checked( $checked, 1, false ) . ' /> ';
+		esc_html_e( 'Enable', 'wplt2' );
+		echo '<p class="description">' . esc_html__( 'Reset plugin settings on re-activation.', 'wplt2' ) . '</p>';
+		echo '</label>';
+	}
+
+	/**
+	 * Render the settings page.
+	 *
+	 * @return void
+	 */
+	public static function settings_page() {
+		?>
+		<div class="wrap">
+			<div id="icon-options-general" class="icon32"><br></div>
+			<h2>Liveticker <?php esc_html_e( 'Settings', 'wplt2' ); ?></h2>
+			<?php if ( isset( $_GET['settings-updated'] ) ) {
+				echo '<div class="updated"><p>' . esc_html__( 'Settings updated successfully.', 'wplt2' ) . '</p></div>';
+} ?>
+			<form action="options.php" method="post">
+				<?php
+				settings_fields( 'wplt2_settings' );
+				do_settings_sections( __FILE__ );
+				?>
+				<p class="submit">
+					<input type="submit" name="submit" id="submit" class="button button-primary" value="Save Changes">
+				</p>
+			</form>
+		</div>
+		<?php
+	}
+
+	/**
+	 * Validate settings callback.
+	 *
+	 * @param array $input Input arguments.
+	 *
+	 * @return array Parsed arguments.
+	 */
+	public static function validate_settings( $input ) {
+		$defaults = self::default_options();
+
+		return wp_parse_args( $input, $defaults );
+	}
 }
diff --git a/includes/class-wpliveticker2.php b/includes/class-wpliveticker2.php
index f5d5126..5fff92a 100644
--- a/includes/class-wpliveticker2.php
+++ b/includes/class-wpliveticker2.php
@@ -62,6 +62,10 @@ class WPLiveticker2 {
 		if ( is_admin() ) {
 			// Add dashboard "right now" functionality.
 			add_action( 'right_now_content_table_end', array( 'WPLiveticker2_Admin', 'dashboard_right_now' ) );
+
+			// Settings.
+			add_action( 'admin_init', array( 'WPLiveticker2_Admin', 'register_settings' ) );
+			add_action( 'admin_menu', array( 'WPLiveticker2_Admin', 'register_settings_page' ) );
 		}
 	}