SGWordPressWidget.php
167 lines
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * Adds Foo_Widget widget. |
| 5 | */ |
| 6 | class SGWordPressWidget extends WP_Widget |
| 7 | { |
| 8 | private $widgetIndex = 0; |
| 9 | |
| 10 | /** |
| 11 | * Register widget with WordPress. |
| 12 | */ |
| 13 | |
| 14 | function __construct() |
| 15 | { |
| 16 | $widgetOptions = array( |
| 17 | 'classname' => 'sg_wordpress_widget', |
| 18 | 'description' => 'Widget for BackupGuard seal', |
| 19 | ); |
| 20 | parent::__construct( |
| 21 | 'sg_wordpress_widget', |
| 22 | esc_html__('BackupGuard Seal', 'text_domain'), |
| 23 | $widgetOptions |
| 24 | ); |
| 25 | } |
| 26 | |
| 27 | /** |
| 28 | * Front-end display of widget. |
| 29 | * |
| 30 | * @see WP_Widget::widget() |
| 31 | * |
| 32 | * @param array $args Widget arguments. |
| 33 | * @param array $instance Saved values from database. |
| 34 | */ |
| 35 | public function widget($args, $instance) |
| 36 | { |
| 37 | $sgSealTheme = isset($instance['theme']) ? $instance['theme'] : ''; |
| 38 | $sgSealImage = isset($instance['theme-'.$sgSealTheme.'-image']) ? strip_tags($instance['theme-'.$sgSealTheme.'-image']) : ''; |
| 39 | |
| 40 | if ($sgSealImage) { |
| 41 | echo '<a href="https://backup-guard.com" style="text-decoration:none;" target="_blank"><img src="https://backup-guard.com/seal/'.$sgSealImage.'" alt="BackupGuard - backup your website in the cloud"></a>'; |
| 42 | } |
| 43 | } |
| 44 | |
| 45 | /** |
| 46 | * Back-end widget form. |
| 47 | * |
| 48 | * @see WP_Widget::form() |
| 49 | * |
| 50 | * @param array $instance Previously saved values from database. |
| 51 | */ |
| 52 | public function form($instance) |
| 53 | { |
| 54 | $sgSealTheme = isset($instance['theme']) ? $instance['theme'] : SG_SEAL_THEME_DARK; |
| 55 | $sgSealImage = isset($instance['theme-'.$sgSealTheme.'-image']) ? $instance['theme-'.$sgSealTheme.'-image'] : '1'; |
| 56 | if ($this->number != "__i__") { |
| 57 | $this->widgetIndex = $this->number; |
| 58 | } |
| 59 | else { |
| 60 | $this->widgetIndex += 1; |
| 61 | } |
| 62 | |
| 63 | ?> |
| 64 | <div class="backup-gaurd-seal-widget-option-container"> |
| 65 | <div style="margin-top: 13px;"> |
| 66 | <label for="<?php echo esc_attr($this->get_field_id('theme').'-'.$this->widgetIndex); ?>"><?php esc_attr_e('Theme:', 'text_domain'); ?></label> |
| 67 | <select id="<?php echo esc_attr($this->get_field_id('theme').'-'.$this->widgetIndex); ?>" name="<?php echo esc_attr($this->get_field_name('theme')); ?>"> |
| 68 | <option value="<?php echo SG_SEAL_THEME_DARK ?>" <?php echo $sgSealTheme == SG_SEAL_THEME_DARK ? "selected" : "" ?>>Dark</option> |
| 69 | <option value="<?php echo SG_SEAL_THEME_GREEN ?>" <?php echo $sgSealTheme == SG_SEAL_THEME_GREEN ? "selected" : "" ?>>Green</option> |
| 70 | <option value="<?php echo SG_SEAL_THEME_WHITE ?>" <?php echo $sgSealTheme == SG_SEAL_THEME_WHITE ? "selected" : "" ?>>White</option> |
| 71 | </select> |
| 72 | </div> |
| 73 | <div id="<?php echo esc_attr($this->get_field_id('theme-dark').'-'.$this->widgetIndex) ?>" <?php echo $sgSealTheme == SG_SEAL_THEME_DARK ? "" : "hidden" ?> style="margin-bottom: 13px; margin-top: 13px;"> |
| 74 | <div style="margin-bottom: 5px;"> |
| 75 | <input type="radio" name="<?php echo esc_attr($this->get_field_name('theme-dark-image')); ?>" value="1" checked> |
| 76 | <img src="<?php echo SG_IMAGE_URL.'1.png' ?>"> |
| 77 | </div> |
| 78 | <div style="margin-bottom: 5px;"> |
| 79 | <input type="radio" name="<?php echo esc_attr($this->get_field_name('theme-dark-image')); ?>" value="4" <?php echo ($sgSealImage == "4") ? "checked":""; ?>> |
| 80 | <img src="<?php echo SG_IMAGE_URL.'4.png' ?>"> |
| 81 | </div> |
| 82 | <div style="margin-bottom: 5px;"> |
| 83 | <input type="radio" name="<?php echo esc_attr($this->get_field_name('theme-dark-image')); ?>" value="5" <?php echo ($sgSealImage == "5") ? "checked":""; ?>> |
| 84 | <img src="<?php echo SG_IMAGE_URL.'5.png' ?>"> |
| 85 | </div> |
| 86 | <div style="margin-bottom: 5px;"> |
| 87 | <input type="radio" name="<?php echo esc_attr($this->get_field_name('theme-dark-image')); ?>" value="8" <?php echo ($sgSealImage == "8") ? "checked":""; ?>> |
| 88 | <img src="<?php echo SG_IMAGE_URL.'8.png' ?>"> |
| 89 | </div> |
| 90 | </div> |
| 91 | <div id="<?php echo esc_attr($this->get_field_id('theme-green').'-'.$this->widgetIndex) ?>" <?php echo $sgSealTheme == SG_SEAL_THEME_GREEN ? "" : "hidden" ?> style="margin-bottom: 13px; margin-top: 13px;"> |
| 92 | <div style="margin-bottom: 5px;"> |
| 93 | <input type="radio" name="<?php echo esc_attr($this->get_field_name('theme-green-image')); ?>" value="2" checked> |
| 94 | <img src="<?php echo SG_IMAGE_URL.'2.png' ?>"> |
| 95 | </div> |
| 96 | <div style="margin-bottom: 5px;"> |
| 97 | <input type="radio" name="<?php echo esc_attr($this->get_field_name('theme-green-image')); ?>" value="6" <?php echo ($sgSealImage == "6") ? "checked":""; ?>> |
| 98 | <img src="<?php echo SG_IMAGE_URL.'6.png' ?>"> |
| 99 | </div> |
| 100 | </div> |
| 101 | <div id="<?php echo esc_attr($this->get_field_id('theme-white').'-'.$this->widgetIndex) ?>" <?php echo $sgSealTheme == SG_SEAL_THEME_WHITE ? "" : "hidden" ?> style="margin-bottom: 13px; margin-top: 13px;"> |
| 102 | <div style="margin-bottom: 5px;"> |
| 103 | <input type="radio" name="<?php echo esc_attr($this->get_field_name('theme-white-image')); ?>" value="3" checked> |
| 104 | <img src="<?php echo SG_IMAGE_URL.'3.png' ?>"> |
| 105 | </div> |
| 106 | <div style="margin-bottom: 5px;"> |
| 107 | <input type="radio" name="<?php echo esc_attr($this->get_field_name('theme-white-image')); ?>" value="7" <?php echo ($sgSealImage == "7") ? "checked":""; ?>> |
| 108 | <img src="<?php echo SG_IMAGE_URL.'7.png' ?>"> |
| 109 | </div> |
| 110 | </div> |
| 111 | </div> |
| 112 | |
| 113 | <style type="text/css"> |
| 114 | .backup-gaurd-seal-widget-option-container input, img { |
| 115 | vertical-align: middle; |
| 116 | } |
| 117 | </style> |
| 118 | |
| 119 | <script type="text/javascript"> |
| 120 | jQuery("select[id*='theme-<?php echo $this->widgetIndex ?>']").on("change", function () { |
| 121 | var theme = jQuery(this).val(); |
| 122 | |
| 123 | if (theme == "dark") { |
| 124 | jQuery( "div[id*='theme-dark-<?php echo $this->widgetIndex ?>']" ).show(); |
| 125 | jQuery( "div[id*='theme-green-<?php echo $this->widgetIndex ?>']" ).hide(); |
| 126 | jQuery( "div[id*='theme-white-<?php echo $this->widgetIndex ?>']" ).hide(); |
| 127 | } |
| 128 | else if (theme == "green") { |
| 129 | jQuery( "div[id*='theme-green-<?php echo $this->widgetIndex ?>']" ).show(); |
| 130 | jQuery( "div[id*='theme-dark-<?php echo $this->widgetIndex ?>']" ).hide(); |
| 131 | jQuery( "div[id*='theme-white-<?php echo $this->widgetIndex ?>']" ).hide(); |
| 132 | } |
| 133 | else { |
| 134 | jQuery( "div[id*='theme-white-<?php echo $this->widgetIndex ?>']" ).show(); |
| 135 | jQuery( "div[id*='theme-dark-<?php echo $this->widgetIndex ?>']" ).hide(); |
| 136 | jQuery( "div[id*='theme-green-<?php echo $this->widgetIndex ?>']" ).hide(); |
| 137 | } |
| 138 | }); |
| 139 | </script> |
| 140 | <?php |
| 141 | } |
| 142 | |
| 143 | /** |
| 144 | * Sanitize widget form values as they are saved. |
| 145 | * |
| 146 | * @see WP_Widget::update() |
| 147 | * |
| 148 | * @param array $new_instance Values just sent to be saved. |
| 149 | * @param array $old_instance Previously saved values from database. |
| 150 | * |
| 151 | * @return array Updated safe values to be saved. |
| 152 | */ |
| 153 | public function update($new_instance, $old_instance) |
| 154 | { |
| 155 | $instance = array(); |
| 156 | $instance['theme'] = isset($new_instance['theme']) ? strip_tags($new_instance['theme']) : ''; |
| 157 | $instance['theme-'.$instance['theme'].'-image'] = isset($new_instance['theme-'.$instance['theme'].'-image']) ? strip_tags($new_instance['theme-'.$instance['theme'].'-image']) : ''; |
| 158 | |
| 159 | if (!$instance['theme-'.$instance['theme'].'-image']) { |
| 160 | return false; |
| 161 | } |
| 162 | |
| 163 | return $instance; |
| 164 | } |
| 165 | |
| 166 | } |
| 167 |