DashboardWidget.php
3 weeks ago
ParsiDateArchiveWidget.php
3 weeks ago
ParsiDateCalendarWidget.php
3 weeks ago
Widget.php
3 weeks ago
ParsiDateCalendarWidget.php
148 lines
| 1 | <?php |
| 2 | /** |
| 3 | * ParsiDate Calendar Widget |
| 4 | * |
| 5 | * Add calendar widget to registered sidebar |
| 6 | */ |
| 7 | |
| 8 | namespace WPParsidate\Widget; |
| 9 | |
| 10 | defined( 'ABSPATH' ) or exit( 'No direct script access allowed' ); |
| 11 | |
| 12 | use WPParsidate\Core\Calendar; |
| 13 | use WPParsidate\Settings\Settings; |
| 14 | |
| 15 | /** |
| 16 | * @author lord_viper |
| 17 | * @copyright 2013 |
| 18 | */ |
| 19 | class ParsiDateCalendarWidget extends \WP_Widget { |
| 20 | public function __construct() { |
| 21 | global $wp_version; |
| 22 | |
| 23 | if ( version_compare( $wp_version, '4.3', '>=' ) ) { |
| 24 | parent::__construct( false, esc_html__( 'Jalali Date Calender', 'wp-parsidate' ), |
| 25 | 'description=' . esc_html__( 'Jalali Date Calender', 'wp-parsidate' ) ); |
| 26 | } else { |
| 27 | $this->WP_Widget( false, esc_html__( 'Jalali Date Calender', 'wp-parsidate' ), |
| 28 | 'description=' . esc_html__( 'Jalali Date Calender', 'wp-parsidate' ) ); |
| 29 | } |
| 30 | } |
| 31 | |
| 32 | /** |
| 33 | * Outputs the settings update form. |
| 34 | * |
| 35 | * @param array $instance Current settings. |
| 36 | * |
| 37 | * @return void Default return is 'noform'. |
| 38 | * @since 2.8.0 |
| 39 | * |
| 40 | */ |
| 41 | public function form( $instance ) { |
| 42 | $title = ! empty( $instance['parsidate_calendar_title'] ) ? $instance['parsidate_calendar_title'] : esc_html__( 'Jalali Date Calender', |
| 43 | 'wp-parsidate' ); |
| 44 | $theme = ! empty( $instance['theme_color'] ) ? $instance['theme_color'] : 'light-mode'; |
| 45 | |
| 46 | if ( ! Settings::get( 'conv_permalinks', false ) ) { |
| 47 | echo "<p style='color: #ff8153'>" . esc_html__( 'For use widget, active "Fix permalinks dates" option in plugin settings.', |
| 48 | 'wp-parsidate' ) . "</p>"; |
| 49 | } |
| 50 | ?> |
| 51 | <p style="text-align:right; direction:rtl"> |
| 52 | <label for="<?php echo esc_attr( $this->get_field_id( 'parsidate_calendar_title' ) ); ?>"> |
| 53 | <?php esc_html_e( 'Title:', 'wp-parsidate' ) ?></label> |
| 54 | |
| 55 | <input style="width:calc(100% - 120px);float:left" |
| 56 | id="<?php echo esc_attr( $this->get_field_id( 'parsidate_calendar_title' ) ); ?>" |
| 57 | name="<?php echo esc_attr( $this->get_field_name( 'parsidate_calendar_title' ) ); ?>" type="text" |
| 58 | value="<?php echo esc_attr( $title ); ?>"/> |
| 59 | </p> |
| 60 | |
| 61 | <p style="text-align:right; direction:rtl"> |
| 62 | <label for="<?php echo esc_attr( $this->get_field_id( 'theme-color' ) ); ?>"> |
| 63 | <?php esc_html_e( 'Theme color:', 'wp-parsidate' ) ?></label> |
| 64 | |
| 65 | <select style="width:calc(100% - 120px);float:left" |
| 66 | id="<?php echo esc_attr( $this->get_field_id( 'theme-color' ) ); ?>" |
| 67 | name="<?php echo esc_attr( $this->get_field_name( 'theme-color' ) ); ?>"> |
| 68 | <option value="light-mode" <?php selected( $theme, 'light-mode' ); ?>> |
| 69 | <?php esc_html_e( 'Light Mode', 'wp-parsidate' ) ?> |
| 70 | </option> |
| 71 | <option value="dark-mode" <?php selected( $theme, 'dark-mode' ); ?>> |
| 72 | <?php esc_html_e( 'Dark Mode', 'wp-parsidate' ) ?> |
| 73 | </option> |
| 74 | </select> |
| 75 | </p> |
| 76 | <?php |
| 77 | } |
| 78 | |
| 79 | /** |
| 80 | * Updates a particular instance of a widget. |
| 81 | * |
| 82 | * This function should check that `$new_instance` is set correctly. The newly-calculated |
| 83 | * value of `$instance` should be returned. If false is returned, the instance won't be |
| 84 | * saved/updated. |
| 85 | * |
| 86 | * @param array $new_instance New settings for this instance as input by the user via |
| 87 | * WP_Widget::form(). |
| 88 | * @param array $old_instance Old settings for this instance. |
| 89 | * |
| 90 | * @return array Settings to save or bool false to cancel saving. |
| 91 | * @since 2.8.0 |
| 92 | * |
| 93 | */ |
| 94 | public function update( $new_instance, $old_instance ) { |
| 95 | $instance = $old_instance; |
| 96 | $instance['parsidate_calendar_title'] = esc_html( $new_instance['parsidate_calendar_title'] ); |
| 97 | $instance['theme_color'] = esc_attr( $new_instance['theme-color'] ); |
| 98 | |
| 99 | return $instance; |
| 100 | } |
| 101 | |
| 102 | /** |
| 103 | * Echoes the widget content. |
| 104 | * |
| 105 | * Subclasses should override this function to generate their widget code. |
| 106 | * |
| 107 | * @param array $args Display arguments including 'before_title', 'after_title', |
| 108 | * 'before_widget', and 'after_widget'. |
| 109 | * @param array $instance The settings for the particular instance of the widget. |
| 110 | * |
| 111 | * @since 2.8.0 |
| 112 | * |
| 113 | */ |
| 114 | public function widget( $args, $instance ) { |
| 115 | if ( ! Settings::get( 'conv_permalinks', false ) ) { |
| 116 | return; |
| 117 | } |
| 118 | |
| 119 | $theme = ! empty( $instance['theme_color'] ) ? $instance['theme_color'] : 'light-mode'; |
| 120 | $widget_id = $args['widget_id']; |
| 121 | |
| 122 | // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped |
| 123 | echo $args['before_widget']; |
| 124 | |
| 125 | if ( ! empty( $instance['parsidate_calendar_title'] ) ) { |
| 126 | // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped |
| 127 | echo $args['before_title']; |
| 128 | // @TODO: Escape widget_title maybe corrupted some theme add custom HTML on widget_title hook |
| 129 | echo esc_html( apply_filters( 'widget_title', $instance['parsidate_calendar_title'] ) ); |
| 130 | // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped |
| 131 | echo $args['after_title']; |
| 132 | } |
| 133 | |
| 134 | Calendar::printCalendar(); |
| 135 | |
| 136 | // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped |
| 137 | echo $args['after_widget']; |
| 138 | |
| 139 | if ( $theme === 'dark-mode' ) { |
| 140 | // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped |
| 141 | echo "<style>#$widget_id{background:#141414;border-radius:8px 8px 4px 4px;" . "overflow:hidden;box-shadow:0 0 5px 0 #000;text-align:center;padding-top:15px;color:#dcdcdc}" . "#$widget_id table{direction:rtl;border-radius:12px;overflow:hidden;" . "background:#1d1d1d;box-shadow:inset 0 0 0 6px #141414}#$widget_id table th," . "#$widget_id table td{border:0}#$widget_id table th:last-child," . "#$widget_id table tr td:last-child{color:#f28a8a}</style>"; |
| 142 | } else { |
| 143 | // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped |
| 144 | echo "<style>#$widget_id{background:#dbdbdb;border-radius:12px;overflow:hidden;" . "box-shadow:0 0 15px 0 #0000004f,inset 0 0 0 1px #8080806e;text-align:center;padding-top:15px;" . "color:#1e1e1e}#$widget_id table{direction:rtl;border-radius:9px;overflow:hidden;" . "background:#fdfdfd;box-shadow:0 -13px 14px 0 #8080801a}#$widget_id table th," . "#$widget_id table td{border:0}#$widget_id table th:last-child," . "#$widget_id table tr td:last-child{color:#bf4a4a}</style>"; |
| 145 | } |
| 146 | } |
| 147 | } |
| 148 |