File: /var/www/html/wp-content/themes/polygant_2023/widgets/widget.php
<?php
// register sidebar
if (function_exists('register_sidebar')) {
register_sidebar(
array(
'name' => 'Footer external links',
'id' => 'footer_external_links',
'description' => 'You can write footer info here',
'before_widget' => '',
'after_widget' => '',
)
);
}
// register widget
add_action('widgets_init', 'poly_external_link_widget');
function poly_external_link_widget()
{
register_widget('poly_external_links');
}
class Poly_External_Links extends WP_Widget
{
function __construct()
{
parent::__construct(
'external_links',
'External links',
array(
'description' => 'External links',
'classname' => 'poly_external_links'
)
);
}
// render widget result in frontend
function widget($args, $instance)
{
$title = apply_filters('widget_title', $instance['title']);
$posts = apply_filters('widget_posts', $instance['posts']);
$visible_url = apply_filters('widget_url', $instance['url']);
$url = apply_filters('widget_url', $instance['url']);
$popup = apply_filters('widget_title', $instance['popup']);
$html = '<div class="dataWrap__content"><a href="' . get_option('siteurl') . '/' . $visible_url . '" class="mnu anchor" data-custom-anchor="' . $url . '">' . $title . '</a></div>';
if ($posts !== 'default' && !empty($posts)) {
$icl_object_id = icl_object_id($posts, 'post', true);
global $sitepress;
$lang = $sitepress->get_current_language() . '/';
$html = '<div class="dataWrap__content"><a href="' . get_the_permalink($icl_object_id) . '" class="mnu">' . $title . '</a></div>';
} else if($popup) {
$html = '<div class="dataWrap__content"><a href="#" class="mnu formPopup">' . $title . '</a></div>';
}
echo $args['before_widget'];
echo $html;
echo $args['after_widget'];
}
// render widget in admin
function form($instance)
{
$title = @ $instance['title'] ?: '';
$visible_url = @ $instance['visible_url'] ?: '';
$url = @ $instance['url'] ?: '';
$posts = @ $instance['posts'] ?: '';
$popup = @ $instance['popup'] ?: '';
$inc = 0;
$query = new WP_Query(array(
'post_type' => array('post', 'skills', 'ai'),
'posts_per_page' => -1,
));
?>
<p>
<label for="<?php echo $this->get_field_id('popup'); ?>">Popup?</label><br/>
<?php if($popup) : ?>
<input class="widefat" id="<?php echo $this->get_field_id('popup'); ?>"
name="<?php echo $this->get_field_name('popup'); ?>" type="checkbox"
value="popup" checked>
<?php else : ?>
<input class="widefat" id="<?php echo $this->get_field_id('popup'); ?>"
name="<?php echo $this->get_field_name('popup'); ?>" type="checkbox"
value="popup">
<?php endif; ?>
</p>
<p>
<label for="<?php echo $this->get_field_id('title'); ?>">Title</label><br/>
<input class="widefat" id="<?php echo $this->get_field_id('title'); ?>"
name="<?php echo $this->get_field_name('title'); ?>" type="text"
value="<?php echo esc_attr($title); ?>">
</p>
<p>
<label for="<?php echo $this->get_field_id('visible_url'); ?>">Visible_url</label>
<input class="widefat" id="<?php echo $this->get_field_id('visible_url'); ?>"
name="<?php echo $this->get_field_name('visible_url'); ?>" type="text"
value="<?php echo esc_attr($visible_url); ?>">
</p>
<p>
<label for="<?php echo $this->get_field_id('url'); ?>">Url</label>
<input class="widefat" id="<?php echo $this->get_field_id('url'); ?>"
name="<?php echo $this->get_field_name('url'); ?>" type="text"
value="<?php echo esc_attr($url); ?>">
</p>
<?php if ($query->have_posts()) : ?>
<p>Or</p>
<p>
<label for="<?php echo $this->get_field_id('posts'); ?>">Posts</label>
<select class="widefat" id="<?php echo $this->get_field_id('posts'); ?>"
name="<?php echo $this->get_field_name('posts'); ?>">
<?php $k = 0; while ($query->have_posts()) : $query->the_post(); ?>
<?php if (isset($posts)) : ?>
<?php if ($inc == 0) : ?>
<option value="default">Select post</option>
<?php endif; ?>
<?php if ($posts == $query->post->ID) : ?>
<option value="<?php echo esc_attr($query->post->ID); ?>" selected>
<?php echo esc_attr($query->post->post_title); ?>
</option>
<?php else : ?>
<option value="<?php echo esc_attr($query->post->ID); ?>">
<?php echo esc_attr($query->post->post_title); ?>
</option>
<?php endif; ?>
<?php else : ?>
<?php if ($inc == 0) : ?>
<option value="default" selected>Select post</option>
<?php else : ?>
<option value="<?php echo esc_attr($query->post->ID); ?>">
<?php echo esc_attr($query->post->post_title); ?>
</option>
<?php endif; ?>
<?php endif; ?>
<?php $inc++; ?>
<?php endwhile;?>
</select>
</p>
<?php endif; ?>
<?php
}
// save widget data
function update($new_instance, $old_instance)
{
$instance = $old_instance;
$instance['title'] = strip_tags($new_instance['title']);
$instance['visible_url'] = strip_tags($new_instance['visible_url']);
$instance['url'] = strip_tags($new_instance['url']);
$instance['posts'] = strip_tags($new_instance['posts']);
$instance['popup'] = strip_tags($new_instance['popup']);
return $instance;
}
}