Settings page restored, AJAX hook bundled in main class

This commit is contained in:
Stefan Kalscheuer 2017-12-07 18:22:21 +01:00
parent 15f02cbe83
commit 32e6db17a0
7 changed files with 97 additions and 64 deletions

View File

@ -1,48 +0,0 @@
<?php
/**
* @package Ajax
*/
// Exit if accessed directly
if ( !defined( 'ABSPATH' ) ) exit;
/**
* Process Ajax upload file
*
* @return void
*/
function wplt_ajax_get_new_ticks() {
check_ajax_referer( 'wplt_ajax_get_new_ticks' );
// timestamp for request
$slug = $_REQUEST['sl'];
$time = $_REQUEST['ts'];
if($slug) {
// get new ticks from database
$args = array( 'post_type' => 'wplt_tick',
'posts_per_page' => '-1',
'tax_query' => array(
array( 'taxonomy' => 'wplt_ticker',
'field' => 'slug',
'terms' => $slug
)
)
);
$wp_query = new WP_Query($args);
$output = '';
while ($wp_query->have_posts()) : $wp_query->the_post();
$output .= '<li class="wplt_tick">
<p><span class="wplt_tick_time">'.get_the_time('d.m.Y H.i').'</span>
<span class="wplt_tick_title">'.get_the_title().'</span></p>
<p class="wplt_tick_content">'.get_the_content().'</p></li>';
endwhile;
// Echo success response
echo $output;
}
die();
}
//add_action( 'wp_ajax_wplt_download_upload', 'wplt_download_upload_ajax' );

View File

@ -55,15 +55,34 @@ class WPLiveticker2_Admin extends WPLiveticker2 {
* @return void
*/
public static function register_settings() {
register_setting( 'wplt2_settings', 'wplt2', array( 'WPLiveticker2_Admin', 'validate_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__ );
add_settings_section(
'wplt2_settings_general',
__( 'General', 'wplt2' ),
array( 'WPLiveticker2_Admin', 'settings_general_section' ),
'wplt2-settings-page'
);
// 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' );
add_settings_field(
'enable_ajax',
__( 'Use AJAX', 'wplt2' ),
array( 'WPLiveticker2_Admin', 'settings_enable_ajax_field' ),
'wplt2-settings-page',
'wplt2_settings_general' );
add_settings_field(
'enable_css',
__( 'Default CSS Styles', 'wplt2' ),
array( 'WPLiveticker2_Admin', 'settings_enable_css_field' ),
'wplt2-settings-page',
'wplt2_settings_general' );
}
/**
@ -90,11 +109,26 @@ class WPLiveticker2_Admin extends WPLiveticker2 {
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 ) . ' /> ';
echo '<label for="' . esc_attr( self::OPTION ) . '[enable_css]">';
echo '<input type="checkbox" name="' . esc_attr( self::OPTION ) . '[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>';
echo '<p class="description">' . esc_html__( 'Disable this option to remove the default styling CSS file.', 'wplt2' ) . '</p>';
}
/**
* Render enable AJAX field.
*
* @return void
*/
public static function settings_enable_ajax_field() {
$checked = self::$_options['enable_ajax'];
echo '<label for="' . esc_attr( self::OPTION ) . '[enable_ajax]">';
echo '<input type="checkbox" name="' . esc_attr( self::OPTION ) . '[enable_ajax]" value="1" ' . checked( $checked, 1, false ) . ' /> ';
esc_html_e( 'Enable', 'wplt2' );
echo '</label>';
echo '<p class="description">' . esc_html__( 'Disable this option to not use AJAX update. This means all liveticker widgets and shortcodes are only updated once on site load.', 'wplt2' ) . '</p>';
}
/**
@ -136,7 +170,7 @@ class WPLiveticker2_Admin extends WPLiveticker2 {
* @return void
*/
public static function settings_page() {
include '../views/settings-page.php';
include WPLT2_DIR . 'views/settings-page.php';
}
/**
@ -148,7 +182,6 @@ class WPLiveticker2_Admin extends WPLiveticker2 {
*/
public static function validate_settings( $input ) {
$defaults = self::default_options();
return wp_parse_args( $input, $defaults );
}
}

View File

@ -129,7 +129,7 @@ class WPLiveticker2_Widget extends WP_Widget {
$ajax = isset( $instance['ajax'] ) ? esc_attr( $instance['ajax'] ) : '0';
$categories = get_terms( 'wplt2_ticker', 'orderby=name&order=ASC' );
include '../views/widget-form.php';
include WPLT2_DIR . 'views/widget-form.php';
}
/**

View File

@ -68,6 +68,11 @@ class WPLiveticker2 {
// Enqueue styles.
add_action( 'wp_footer', array( 'WPLiveticker2', 'enqueue_styles' ) );
// Add AJAX hook if configured.
if ( 1 === self::$_options['enable_ajax'] ) {
add_action( 'wp_ajax_wplt2_update-ticks', array( 'WPLiveticker2', 'ajax_update' ) );
}
// Admin only actions.
if ( is_admin() ) {
// Add dashboard "right now" functionality.
@ -207,6 +212,50 @@ class WPLiveticker2 {
}
}
/**
* Process Ajax upload file
*
* @return void
*/
public static function ajax_update() {
// TODO: re-enable security checks.
// check_ajax_referer( 'wplt_ajax_get_new_ticks' );
// Timestamp for request.
$slug = $_REQUEST['sl'];
$time = $_REQUEST['ts'];
if ( $slug ) {
// get new ticks from database
$args = array(
'post_type' => 'wplt_tick',
'posts_per_page' => '-1',
'tax_query' => array(
array(
'taxonomy' => 'wplt_ticker',
'field' => 'slug',
'terms' => $slug
)
)
);
$wp_query = new WP_Query( $args );
$output = '';
while ( $wp_query->have_posts() ) {
$wp_query->the_post();
$output .= '<li class="wplt_tick">'
. '<p><span class="wplt_tick_time">' . get_the_time( 'd.m.Y H.i' ) . '</span>'
. '<span class="wplt_tick_title">' . get_the_title() . '</span></p>'
. '<p class="wplt_tick_content">' . get_the_content() . '</p></li>';
}
// Echo success response
echo $output;
}
die();
}
/**
* Update options.
*

View File

@ -20,10 +20,8 @@ defined( 'ABSPATH' ) || exit;
<form action="options.php" method="post">
<?php
settings_fields( 'wplt2_settings' );
do_settings_sections( __FILE__ );
do_settings_sections( 'wplt2-settings-page' );
submit_button();
?>
<p class="submit">
<input type="submit" name="submit" id="submit" class="button button-primary" value="Save Changes">
</p>
</form>
</div>

View File

@ -108,7 +108,7 @@ defined( 'ABSPATH' ) || exit;
<label for="<?php echo esc_attr( $this->get_field_id( 'highlight_time' ) ); ?>"><?php esc_html_e( 'Highlight time [s]:', 'wplt2' ); ?></label>
</td>
<td>
<input class="widefat" id="<?php echo esc_attr( $this->get_field_id( 'highlight_time' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'highlight_time' ) ); ?>" type="text" value="<?php echo esc_html( $highlight_time ); ?>" />
<input type="number" class="widefat" id="<?php echo esc_attr( $this->get_field_id( 'highlight_time' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'highlight_time' ) ); ?>" type="text" value="<?php echo esc_html( $highlight_time ); ?>" />
</td>
</tr>
<tr>

View File

@ -37,6 +37,7 @@ defined( 'ABSPATH' ) || exit;
define( 'WPLT2_FILE', __FILE__ );
define( 'WPLT2_DIR', plugin_dir_path( __FILE__ ) );
define( 'WPLT2_BASE', plugin_dir_url( __FILE__ ) );
define( 'WPLT2_BASENAME', plugin_basename( __FILE__ ) );
// System Hooks.
add_action( 'init', array( 'WPLiveticker2', 'register_types' ) );