| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* TaxoPress widget class |
| 5 |
* |
| 6 |
*/ |
| 7 |
class SimpleTags_RelatedPosts_Widget extends WP_Widget |
| 8 |
{ |
| 9 |
/** |
| 10 |
* Constructor widget |
| 11 |
* |
| 12 |
* @return void |
| 13 |
* @author Olatechpro |
| 14 |
*/ |
| 15 |
public function __construct() |
| 16 |
{ |
| 17 |
parent::__construct( |
| 18 |
'simpletags-relatedposts', |
| 19 |
esc_html__('Related Posts (TaxoPress)', 'simple-tags'), |
| 20 |
[ |
| 21 |
'classname' => 'widget-simpletags-relatedposts', |
| 22 |
'description' => esc_html__('Taxopress Related Posts Shortcode', 'simple-tags') |
| 23 |
] |
| 24 |
); |
| 25 |
} |
| 26 |
|
| 27 |
/** |
| 28 |
* Default settings for widget |
| 29 |
* |
| 30 |
* @return array |
| 31 |
* @author Olatechpro |
| 32 |
*/ |
| 33 |
public static function get_fields() |
| 34 |
{ |
| 35 |
return [ |
| 36 |
'relatedposts_id' => 0, |
| 37 |
]; |
| 38 |
} |
| 39 |
|
| 40 |
/** |
| 41 |
* Method for theme render |
| 42 |
* |
| 43 |
* @param array $args |
| 44 |
* @param array $instance |
| 45 |
* |
| 46 |
* @return void |
| 47 |
* @author Olatechpro |
| 48 |
*/ |
| 49 |
public function widget($args, $instance) |
| 50 |
{ |
| 51 |
extract($args); |
| 52 |
|
| 53 |
// Set values and clean it |
| 54 |
foreach ((array)self::get_fields() as $field => $field_value) { |
| 55 |
${$field} = isset($instance[$field]) ? trim($instance[$field]) : ''; |
| 56 |
}//$relatedposts_id; |
| 57 |
|
| 58 |
// phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped |
| 59 |
echo $before_widget; |
| 60 |
// phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped |
| 61 |
echo do_shortcode('[taxopress_relatedposts id="' . (int)$relatedposts_id . '"]'); |
| 62 |
// phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped |
| 63 |
echo $after_widget; |
| 64 |
} |
| 65 |
|
| 66 |
/** |
| 67 |
* Update widget settings |
| 68 |
* |
| 69 |
* @param array $new_instance |
| 70 |
* @param array $old_instance |
| 71 |
* |
| 72 |
* @return array |
| 73 |
* @author Olatechpro |
| 74 |
*/ |
| 75 |
public function update($new_instance, $old_instance) |
| 76 |
{ |
| 77 |
$instance = $old_instance; |
| 78 |
|
| 79 |
foreach ((array)self::get_fields() as $field => $field_value) { |
| 80 |
$instance[$field] = $new_instance[$field]; |
| 81 |
} |
| 82 |
|
| 83 |
return $instance; |
| 84 |
} |
| 85 |
|
| 86 |
/** |
| 87 |
* Admin form for widgets |
| 88 |
* |
| 89 |
* @param array $instance |
| 90 |
* |
| 91 |
* @return void |
| 92 |
* @author Olatechpro |
| 93 |
*/ |
| 94 |
public function form($instance) |
| 95 |
{ |
| 96 |
//Defaults |
| 97 |
$instance = wp_parse_args((array)$instance, self::get_fields()); |
| 98 |
|
| 99 |
$relatedposts_data = taxopress_get_relatedpost_data(); |
| 100 |
if (count($relatedposts_data) > 0) { |
| 101 |
$shortcode_page = sprintf( |
| 102 |
'<a href="%s">%s</a>', |
| 103 |
add_query_arg( |
| 104 |
[ |
| 105 |
'page' => 'st_related_posts', |
| 106 |
], |
| 107 |
admin_url('admin.php') |
| 108 |
), |
| 109 |
esc_html__('this page.', 'simple-tags') |
| 110 |
); |
| 111 |
|
| 112 |
echo '<p>' . esc_html__('Related Posts are added on ', 'simple-tags'); |
| 113 |
// phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped |
| 114 |
echo $shortcode_page; |
| 115 |
echo '</p>' |
| 116 |
|
| 117 |
?> |
| 118 |
<p> |
| 119 |
<label for="<?php echo esc_attr($this->get_field_id('relatedposts_id')); ?>"> |
| 120 |
<?php esc_html_e('Select widget Related Posts.', 'simple-tags'); ?> |
| 121 |
<select id="<?php echo esc_attr($this->get_field_id('relatedposts_id')); ?>" |
| 122 |
name="<?php echo esc_attr($this->get_field_name('relatedposts_id')); ?>"> |
| 123 |
<?php foreach ($relatedposts_data as $key => $value) { ?> |
| 124 |
<option <?php selected(esc_attr($instance['relatedposts_id']), esc_attr($key)); ?> |
| 125 |
value="<?php echo esc_attr($key); ?>"><?php echo esc_html($value['title']); ?></option> |
| 126 |
<?php } ?> |
| 127 |
</select> |
| 128 |
</label> |
| 129 |
</p> |
| 130 |
|
| 131 |
<?php |
| 132 |
} else { |
| 133 |
$shortcode_page = sprintf( |
| 134 |
'<a href="%s">%s</a>', |
| 135 |
add_query_arg( |
| 136 |
[ |
| 137 |
'page' => 'st_related_posts', |
| 138 |
], |
| 139 |
admin_url('admin.php') |
| 140 |
), |
| 141 |
esc_html__('Here', 'simple-tags') |
| 142 |
); |
| 143 |
|
| 144 |
echo '<br />' . esc_html__('No Related Posts shortcode available. Add new shortcode ', 'simple-tags'); |
| 145 |
// phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped |
| 146 |
echo $shortcode_page; |
| 147 |
echo '<br /><br />'; |
| 148 |
} |
| 149 |
} |
| 150 |
} |
| 151 |
|