DashboardWidget.php
3 weeks ago
ParsiDateArchiveWidget.php
3 weeks ago
ParsiDateCalendarWidget.php
3 weeks ago
Widget.php
3 weeks ago
ParsiDateArchiveWidget.php
213 lines
| 1 | <?php |
| 2 | /** |
| 3 | * ParsiDate Archive Widget |
| 4 | * |
| 5 | * Add archive 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\Archive; |
| 13 | use WPParsidate\Settings\Settings; |
| 14 | |
| 15 | /** |
| 16 | * @author lord_viper |
| 17 | * @copyright 2013 |
| 18 | */ |
| 19 | class ParsiDateArchiveWidget extends \WP_Widget { |
| 20 | public function __construct() { |
| 21 | global $wp_version; |
| 22 | |
| 23 | // backwards compatibility |
| 24 | if ( version_compare( $wp_version, '4.3', '>=' ) ) { |
| 25 | parent::__construct( false, esc_html__( 'Jalali Date Archives', 'wp-parsidate' ), |
| 26 | 'description=' . esc_html__( 'Jalali Date Archives', 'wp-parsidate' ) ); |
| 27 | } else { |
| 28 | $this->WP_Widget( false, esc_html__( 'Jalali Date Archives', 'wp-parsidate' ), |
| 29 | 'description=' . esc_html__( 'Jalali Date Archives', 'wp-parsidate' ) ); |
| 30 | } |
| 31 | } |
| 32 | |
| 33 | /** |
| 34 | * Outputs the settings update form. |
| 35 | * |
| 36 | * @param array $instance Current settings. |
| 37 | * |
| 38 | * @return void Default return is 'noform'. |
| 39 | * @return void |
| 40 | * @since 2.8.0 |
| 41 | */ |
| 42 | public function form( $instance ) { |
| 43 | $type = $instance['parsidate_archive_type'] ?? 'monthly'; |
| 44 | $instance['parsidate_archive_title'] = isset( $instance['parsidate_archive_title'] ) ? wp_strip_all_tags( $instance['parsidate_archive_title'] ) : esc_html__( 'Jalali Date Archives', |
| 45 | 'wp-parsidate' ); |
| 46 | $instance['parsidate_archive_count'] = $instance['parsidate_archive_count'] ?? 0; |
| 47 | $instance['parsidate_archive_list'] = $instance['parsidate_archive_list'] ?? 0; |
| 48 | ?> |
| 49 | <p style="text-align:right; direction:rtl"> |
| 50 | |
| 51 | <label |
| 52 | for="<?php echo esc_attr( $this->get_field_id( 'parsidate_archive_title' ) ); ?>"><?php esc_html_e( 'Title', |
| 53 | 'wp-parsidate' ) ?>:</label> |
| 54 | |
| 55 | <input style="width: 200px;" id="<?php echo esc_attr( $this->get_field_id( 'parsidate_archive_title' ) ); ?>" |
| 56 | name="<?php echo esc_attr( $this->get_field_name( 'parsidate_archive_title' ) ); ?>" type="text" |
| 57 | value="<?php echo( empty( $instance['parsidate_archive_title'] ) ? esc_html__( 'Jalali Date Archives', |
| 58 | 'wp-parsidate' ) : esc_html( $instance['parsidate_archive_title'] ) ) ?>"/> |
| 59 | |
| 60 | <br><br> |
| 61 | |
| 62 | <span><?php esc_html_e( 'How to display', 'wp-parsidate' ) ?>:</span><br> |
| 63 | |
| 64 | <label> |
| 65 | <input type="radio" id="parsidate_archive_type1" |
| 66 | name="<?php echo esc_attr( $this->get_field_name( 'parsidate_archive_type' ) ); ?>" |
| 67 | value="yearly" <?php checked( $type, 'yearly' ); ?>/> |
| 68 | <?php esc_html_e( 'Yearly', 'wp-parsidate' ) ?> |
| 69 | </label> |
| 70 | |
| 71 | <br/> |
| 72 | |
| 73 | <label> |
| 74 | <input type="radio" id="parsidate_archive_type2" |
| 75 | name="<?php echo esc_attr( $this->get_field_name( 'parsidate_archive_type' ) ); ?>" |
| 76 | value="monthly" <?php checked( $type, 'monthly' ); ?>/> |
| 77 | <?php esc_html_e( 'Monthly', 'wp-parsidate' ) ?> |
| 78 | </label> |
| 79 | |
| 80 | <br/> |
| 81 | |
| 82 | <label> |
| 83 | <input type="radio" id="parsidate_archive_type3" |
| 84 | name="<?php echo esc_attr( $this->get_field_name( 'parsidate_archive_type' ) ); ?>" |
| 85 | value="weekly" <?php checked( $type, 'weekly' ); ?>/> |
| 86 | <?php esc_html_e( 'Weekly', 'wp-parsidate' ) ?> |
| 87 | </label> |
| 88 | |
| 89 | <br/> |
| 90 | |
| 91 | <label> |
| 92 | <input type="radio" id="parsidate_archive_type4" |
| 93 | name="<?php echo esc_attr( $this->get_field_name( 'parsidate_archive_type' ) ); ?>" |
| 94 | value="daily" <?php checked( $type, 'daily' ); ?>/> |
| 95 | <?php esc_html_e( 'Daily', 'wp-parsidate' ) ?> |
| 96 | </label> |
| 97 | |
| 98 | <br/> |
| 99 | <br/> |
| 100 | |
| 101 | <input type="checkbox" name="<?php echo esc_attr( $this->get_field_name( 'parsidate_archive_count' ) ); ?>" |
| 102 | id="<?php echo esc_attr( $this->get_field_id( 'parsidate_archive_count' ) ); ?>" |
| 103 | value="1" <?php checked( $instance['parsidate_archive_count'], 1 ); ?>/> |
| 104 | |
| 105 | <label for="<?php echo esc_attr( $this->get_field_id( 'parsidate_archive_count' ) ); ?>"> |
| 106 | <?php esc_html_e( 'Show post counts', 'wp-parsidate' ) ?> |
| 107 | </label> |
| 108 | |
| 109 | <br/> |
| 110 | |
| 111 | <input type="checkbox" name="<?php echo esc_attr( $this->get_field_name( 'parsidate_archive_list' ) ); ?>" |
| 112 | id="<?php echo esc_attr( $this->get_field_id( 'parsidate_archive_list' ) ); ?>" |
| 113 | value="1" <?php echo checked( $instance['parsidate_archive_list'], 1 ); ?>/> |
| 114 | |
| 115 | <label for="<?php echo esc_attr( $this->get_field_id( 'parsidate_archive_list' ) ); ?>"> |
| 116 | <?php esc_html_e( 'Display as dropdown', 'wp-parsidate' ) ?> |
| 117 | </label> |
| 118 | |
| 119 | </p> |
| 120 | <?php |
| 121 | if ( ! Settings::get( 'conv_permalinks', false ) ) { |
| 122 | echo "<p style='color: #ff8153'>" . |
| 123 | esc_html__( 'For use widget, active "Fix permalinks dates" option in plugin settings.', 'wp-parsidate' ) . |
| 124 | "</p>"; |
| 125 | } |
| 126 | } |
| 127 | |
| 128 | /** |
| 129 | * Updates a particular instance of a widget. |
| 130 | * |
| 131 | * This function should check that `$new_instance` is set correctly. The newly-calculated |
| 132 | * value of `$instance` should be returned. If false is returned, the instance won't be |
| 133 | * saved/updated. |
| 134 | * |
| 135 | * @param array $new_instance New settings for this instance as input by the user via |
| 136 | * WP_Widget::form(). |
| 137 | * @param array $old_instance Old settings for this instance. |
| 138 | * |
| 139 | * @return array Settings to save or bool false to cancel saving. |
| 140 | * @since 2.8.0 |
| 141 | * |
| 142 | */ |
| 143 | public function update( $new_instance, $old_instance ) { |
| 144 | $instance = $old_instance; |
| 145 | $instance['parsidate_archive_title'] = isset( $new_instance['parsidate_archive_title'] ) ? wp_strip_all_tags( $new_instance['parsidate_archive_title'] ) : esc_html__( 'Jalali Date Archives', |
| 146 | 'wp-parsidate' ); |
| 147 | $instance['parsidate_archive_count'] = $new_instance['parsidate_archive_count'] ?? 0; |
| 148 | $instance['parsidate_archive_list'] = $new_instance['parsidate_archive_list'] ?? 0; |
| 149 | $instance['parsidate_archive_type'] = $new_instance['parsidate_archive_type'] ?? 'monthly'; |
| 150 | |
| 151 | return $instance; |
| 152 | } |
| 153 | |
| 154 | /** |
| 155 | * Echoes the widget content. |
| 156 | * |
| 157 | * Subclasses should override this function to generate their widget code. |
| 158 | * |
| 159 | * @param array $args Display arguments including 'before_title', 'after_title', |
| 160 | * 'before_widget', and 'after_widget'. |
| 161 | * @param array $instance The settings for the particular instance of the widget. |
| 162 | * |
| 163 | * @since 2.8.0 |
| 164 | * |
| 165 | */ |
| 166 | public function widget( $args, $instance ) { |
| 167 | if ( ! Settings::get( 'conv_permalinks', false ) ) { |
| 168 | return; |
| 169 | } |
| 170 | |
| 171 | $type = $instance['parsidate_archive_type'] ?? 'monthly'; |
| 172 | $title = $instance['parsidate_archive_title'] ?? esc_html__( 'Jalali Date Archives', |
| 173 | 'wp-parsidate' ); |
| 174 | $post_count = $instance['parsidate_archive_count'] ?? false; |
| 175 | $ddl_style = isset( $instance['parsidate_archive_list'] ) && $instance['parsidate_archive_list']; |
| 176 | |
| 177 | // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped |
| 178 | echo $args['before_widget']; |
| 179 | if ( ! empty( $instance['parsidate_archive_title'] ) ) { |
| 180 | // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped |
| 181 | echo $args['before_title']; |
| 182 | // @TODO: Escape widget_title maybe corrupted some theme add custom HTML on widget_title hook |
| 183 | echo esc_html( apply_filters( 'widget_title', $instance['parsidate_archive_title'] ) ); |
| 184 | // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped |
| 185 | echo $args['after_title']; |
| 186 | } |
| 187 | |
| 188 | if ( $ddl_style ) { |
| 189 | echo "<select name='parsidate_archive_list' onchange='document.location.href=this.options[this.selectedIndex].value;'> <option value='0'>" . esc_attr( $title ) . "</option>"; |
| 190 | |
| 191 | Archive::getPostArchives( array( |
| 192 | 'type' => $type, |
| 193 | 'format' => 'option', |
| 194 | 'show_post_count' => $post_count |
| 195 | ) ); |
| 196 | |
| 197 | echo '</select>'; |
| 198 | } else { |
| 199 | echo '<ul>'; |
| 200 | |
| 201 | Archive::getPostTypeArchives( array( |
| 202 | 'type' => $type, |
| 203 | 'show_post_count' => $post_count |
| 204 | ) ); |
| 205 | |
| 206 | echo '</ul>'; |
| 207 | } |
| 208 | |
| 209 | // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped |
| 210 | echo $args['after_widget']; |
| 211 | } |
| 212 | } |
| 213 |