-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsidebar.php
More file actions
115 lines (93 loc) · 3.51 KB
/
Copy pathsidebar.php
File metadata and controls
115 lines (93 loc) · 3.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
<?php
/**
* Виджет консультаций для сайдбара
*
* Отображает информацию о враче на страницах консультаций
*/
class PharmaWidget extends WP_Widget {
function __construct() {
parent::__construct(
'pharma-sidebar', // Base ID
'Консультации в сайдбаре' // Name
);
add_action( 'widgets_init', function() {
register_widget( 'PharmaWidget' );
});
}
public $args = array(
'before_title' => '<h4 class="widgettitle">',
'after_title' => '</h4>',
'before_widget' => '<div class="widget-wrap">',
'after_widget' => '</div></div>'
);
/**
* Отображение виджета
*
* Формирует ссылки на анкету и страницу врача
*
* @param array $args Параметры виджета
* @param array $instance Настройки виджета
*/
public function widget( $args, $instance ) {
global $post;
if ($post->post_type !== Pharma::CONSULTATION_POST_TYPE) return;
echo $args['before_widget'];
if ( ! empty( $instance['title'] ) ) {
echo $args['before_title'] . apply_filters( 'widget_title', $instance['title'] ) . $args['after_title'];
}
echo '<div class="textwidget">';
$doctor_id = $post->doctor_id;
$query = new WP_Query([
'author' => $doctor_id,
'cat' => QUESTIONARIES_CATEGORY,
]);
if ($query->have_posts()){
$qurl = get_permalink($query->post->ID);
}
$query = new WP_Query([
'author' => $doctor_id,
'cat' => Pharma::$advert_category,
]);
if ($query->have_posts()){
$doctorurl = get_permalink($query->post->ID);
}
$instance['text'] = str_replace(['%doctorurl%', '%qurl%'] , [$doctorurl, $qurl], $instance['text']);
echo $instance['text'];
echo '</div>';
echo $args['after_widget'];
}
/**
* Форма настройки виджета в админке
*
* @param array $instance Текущие настройки виджета
*/
public function form( $instance ) {
$title = ! empty( $instance['title'] ) ? $instance['title'] : esc_html__( '', 'text_domain' );
$text = ! empty( $instance['text'] ) ? $instance['text'] : esc_html__( '', 'text_domain' );
?>
<p>
<label for="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>"><?php echo esc_html__( 'Title:', 'text_domain' ); ?></label>
<input class="widefat" id="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'title' ) ); ?>" type="text" value="<?php echo esc_attr( $title ); ?>">
</p>
<p>
<label for="<?php echo esc_attr( $this->get_field_id( 'Text' ) ); ?>"><?php echo esc_html__( 'Text:', 'text_domain' ); ?></label>
<textarea class="widefat" id="<?php echo esc_attr( $this->get_field_id( 'text' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'text' ) ); ?>" type="text" cols="30" rows="10"><?php echo esc_attr( $text ); ?></textarea>
</p>
<?php
}
/**
* Обновление настроек виджета
*
* @param array $new_instance Новые настройки
* @param array $old_instance Старые настройки
* @return array Обновлённые настройки
*/
public function update( $new_instance, $old_instance ) {
$instance = array();
$instance['title'] = ( !empty( $new_instance['title'] ) ) ? strip_tags( $new_instance['title'] ) : '';
$instance['text'] = ( !empty( $new_instance['text'] ) ) ? $new_instance['text'] : '';
return $instance;
}
}
$my_widget = new PharmaWidget();
?>