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