<?php

/*
Plugin Name: Day Of Week
Plugin URI: http://broadcastwidgets.com/dayofweek
Description: Show different content for each day of the week.
Version: 1.1.4
Author: Broadcast Widgets LLC
Author URI: http://broadcastwidgets.com
License: GPLV2

Copyright 2017 Broadcast Widgets LLC (email : techsupport@broadcastwidgets.com)
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/





$dir = plugin_dir_path( __FILE__ );

//include($dir.'options.php');

function bwdow_options_page_html()
{
	// check user capabilities
	if (!current_user_can('manage_options')) {
		return;
	}
	?>
	<div class="wrap">
		<h1><?= esc_html(get_admin_page_title()); ?></h1>
		<form action="options.php" method="post">
			<?php
			// output security fields for the registered setting "wporg_options"
			settings_fields('bwdow_options');
			$bwdow_options = get_option('bwdow_options');
			// output setting sections and their fields
			// (sections are registered for "wporg", each field is registered to a specific section)
			do_settings_sections('bwdow');
            $settings = array( 'media_buttons' => true, 'textarea_name' => 'bwdow_options[monday]',
                               'textarea_rows' => '7', 'wpautop' => false);
            $tzlist = DateTimeZone::listIdentifiers(DateTimeZone::ALL);
            $serverTimeZone = date_default_timezone_get();
            if (!isset($bwdow_options['timezone'])){
                $bwdow_options['timezone'] = 'UTC';
            }
			?>
            <table>
                <tr>
                    <td></td>
                    <td>
                        <b>Instructions</b><br>
                        <p>1. Please ensure the correct timezone is set below. </p>
                        <p>2. Enter text for each day below</p>
                        <p>3. In any post or page, use the shortcode [showday]</p>
                        <p><strong>Widget Support</strong></p>
                        <p>To use a widget, look in Widgets and you will see <strong>Day Of Week</strong>. You can add a Title to it and this will be shown above the days content.</p>
                        <p><strong>WE WANT TO HEAR FROM YOU</strong>, we have received many comments and ideas over the last few months, if you are interested in more control such as start/end dates and times please let us know here. <a href="http://broadcastwidgets.com/plugindevelop/">Day Of Week Feedback/Comments</a></p>
                        <p><strong>DAY OF WEEK PRO</strong> is in development.</p>
                        <p>That's it. Thanks. If you require support please email <a href="mailto:techsupport@broadcastwidgets.com">techsupport@broadcastwidgets.com</a></p>
                    </td>
                </tr>
                <tr>
                    <td valign="top"><b>TimeZone</b></td>
                    <td>
                        Your server is currently defaulting to <?php echo $serverTimeZone ?>.<br>
                        If you need to change for this plugin, please select below: <br>

                        <select name="bwdow_options[timezone]">
                        <?php
                        foreach ($tzlist as $tz) {
                            $selected = '';
                            if ($bwdow_options['timezone']==$tz){
                                $selected = ' selected';
                            }

                            echo '<option value="'.$tz.'"'.$selected.'>'.$tz.'</option>';
                        }
                        ?>
                        </select>

                    </td>
                </tr>
                <tr>
                    <td><b>Monday</b></td>
                    <td>
                        <?php echo wp_editor(html_entity_decode(stripcslashes($bwdow_options['monday'])),'bwdowmonday', $settings); ?>
                    </td>
                </tr>
                <?php $settings['textarea_name'] = 'bwdow_options[tuesday]'; ?>
                <tr>
                    <td><b>Tuesday</b></td>
                    <td>
                        <?php echo wp_editor(html_entity_decode(stripcslashes($bwdow_options['tuesday'])),'bwdowtuesday', $settings); ?>
                    </td>
                </tr>
                <?php $settings['textarea_name'] = 'bwdow_options[wednesday]'; ?>
                <tr>
                    <td><b>Wednesday</b></td>
                    <td>
                        <?php echo wp_editor(html_entity_decode(stripcslashes($bwdow_options['wednesday'])),'bwdowwednesday', $settings); ?>
                    </td>
                </tr>
                <?php $settings['textarea_name'] = 'bwdow_options[thursday]'; ?>
                <tr>
                    <td><b>Thursday</b></td>
                    <td>
                        <?php echo wp_editor(html_entity_decode(stripcslashes($bwdow_options['thursday'])),'bwdowthursday', $settings); ?>
                    </td>
                </tr>
                <?php $settings['textarea_name'] = 'bwdow_options[friday]'; ?>
                <tr>
                    <td><b>Friday</b></td>
                    <td>
                        <?php echo wp_editor(html_entity_decode(stripcslashes($bwdow_options['friday'])),'bwdowfriday', $settings); ?>
                    </td>
                </tr>
                <?php $settings['textarea_name'] = 'bwdow_options[saturday]'; ?>
                <tr>
                    <td><b>Saturday</b></td>
                    <td>
                        <?php echo wp_editor(html_entity_decode(stripcslashes($bwdow_options['saturday'])),'bwdowsaturday', $settings); ?>
                    </td>
                </tr>
                <?php $settings['textarea_name'] = 'bwdow_options[sunday]'; ?>
                <tr>
                    <td><b>Sunday</b></td>
                    <td>
                        <?php echo wp_editor(html_entity_decode(stripcslashes($bwdow_options['sunday'])),'bwdowsunday', $settings); ?>
                    </td>
                </tr>
                <tr>
                    <td></td>
                    <td>Version: 1.1.4</td>
                </tr>
            </table>


			<?php
			// output save settings button
			submit_button('Save Settings');
			?>
		</form>
	</div>
	<?php
}

function sked_options_page()
{
	add_menu_page(
		'Day of Week',
		'Day of Week',
		'manage_options',
		'bwdow',
		'bwdow_options_page_html',
		'dashicons-calendar-alt',
		20
	);
}

function showDayShows()
{
    $bwdow_options = get_option('bwdow_options');
    if ($bwdow_options['timezone'] != '') {
        $bwdow_szTimezone = new DateTimeZone($bwdow_options['timezone']);
    } else {
        $bwdow_szTimezone = new DateTimeZone('America/New_York');
    }
    $bwdow_DateTime = new DateTime('now', $bwdow_szTimezone);
    $day = $bwdow_DateTime->format('w');
    switch ($day) {
        case 0:
            $bwdow = $bwdow_options['sunday'];
            break;
        case 1:
            $bwdow = $bwdow_options['monday'];
           break;
        case 2:
            $bwdow = $bwdow_options['tuesday'];
           break;
        case 3:
            $bwdow = $bwdow_options['wednesday'];
           break;
        case 4:
            $bwdow = $bwdow_options['thursday'];
            break;
        case 5:
            $bwdow = $bwdow_options['friday'];
            break;
        case 6:
            $bwdow = $bwdow_options['saturday'];
            break;
    }

    return $bwdow;
}

function bwdow_register_settings()
{
    register_setting('bwdow_options','bwdow_options','bwdow_sanitize');
}

function bwdow_sanitize ($input) {
    $input['monday'] = wp_kses_post($input['monday']);
    $input['tuesday'] = wp_kses_post($input['tuesday']);
    $input['wednesday'] = wp_kses_post($input['wednesday']);
    $input['thursday'] = wp_kses_post($input['thursday']);
    $input['friday'] = wp_kses_post($input['friday']);
    $input['saturday'] = wp_kses_post($input['saturday']);
    $input['sunday'] = wp_kses_post($input['sunday']);
    return $input;

}

class dayOfWeek_Widget extends WP_Widget {
    public function __construct()
    {
        parent::__construct('dayofweek_widget', 'Day Of Week',
                            array('description' => 'Shows content from the Day Of Week plugin')
            );
    }

    public $args = array(
        'before_title'  => '<h4 class="widgettitle">',
        'after_title'   => '</h4>',
        'before_widget' => '<div class="widget-wrap">',
        'after_widget'  => '</div></div>'
    );

    public function widget( $args, $instance ) {

        echo $args['before_widget'];

        if ( ! empty( $instance['title'] ) ) {
            echo $args['before_title'] . apply_filters( 'widget_title', $instance['title'] ) . $args['after_title'];
        }

        echo '<div class="textwidget">';

        echo showDayShows();

        echo '</div>';

        echo $args['after_widget'];

    }

    public function form( $instance ) {
        if ( isset( $instance[ 'title' ] ) ) {
            $title = $instance[ 'title' ];
        }
        else {
            $title = '';
        }
        ?>
        <p>
            <label for="<?php echo $this->get_field_name( 'title' ); ?>"><?php _e( 'Title:' ); ?></label>
            <input class="widefat" id="<?php echo $this->get_field_id( 'title' ); ?>" name="<?php echo $this->get_field_name( 'title' ); ?>" type="text" value="<?php echo esc_attr( $title ); ?>" />
        </p>
        <?php
    }

    public function update( $new_instance, $old_instance ) {
        $instance = array();
        $instance['title'] = ( !empty( $new_instance['title'] ) ) ? strip_tags( $new_instance['title'] ) : '';

        return $instance;
    }
}


add_action('admin_menu', 'sked_options_page');
add_shortcode('showday','showDayShows');
add_action('admin_init', 'bwdow_register_settings');
add_action( 'widgets_init', function() { register_widget( 'dayOfWeek_Widget' ); } );
?>
