';
// Handle display styles
if (!empty($settings['display_styles'])) {
if (in_array('icon', $settings['display_styles'])) {
$views_html .= '
';
}
if (in_array('label', $settings['display_styles'])) {
$views_html .= '' . esc_html($settings['views_label']) . '';
}
}
// Add the view count
$views_html .= '' . esc_html(get_post_meta(get_the_ID(), 'advico_views', true) ?? '0') . '';
$views_html .= '';
$position = $settings['position'] ?? 'after';
switch ($position) {
case 'before':
return $views_html . $content;
case 'after':
return $content . $views_html;
case 'manual':
default:
return $content;
}
}
public function enqueue_script()
{
Assets\enqueue_asset(
ADVICO_PLUGIN_DIR . '/assets/frontend/dist',
self::DEV_SCRIPT,
$this->get_config()
);
wp_localize_script(self::HANDLE, self::OBJ_NAME, $this->get_data());
}
/**
* Get the script configuration.
*
* @return array The script configuration.
*/
public function get_config()
{
return array(
'dependencies' => array('react', 'react-dom'),
'handle' => self::HANDLE,
'in-footer' => true,
);
}
/**
* Get data for script localization.
*
* @return array The localized script data.
*/
public function get_data()
{
return array(
'isAdmin' => is_admin(),
'apiUrl' => rest_url() . ADVICO_ROUTE_PREFIX,
'postId' => get_the_ID(),
'userInfo' => $this->get_user_data(),
'nonce' => wp_create_nonce('wp_rest'),
);
}
/**
* Get user data for script localization.
*
* @return array The user data.
*/
private function get_user_data()
{
$username = '';
$avatar_url = '';
$user_role = '';
if (is_user_logged_in()) {
$current_user = wp_get_current_user();
// Get username.
$username = $current_user->user_login;
// Get avatar URL.
$avatar_url = get_avatar_url($current_user->ID);
// Get the first role (if available).
if (! empty($current_user->roles) && is_array($current_user->roles)) {
$user_role = $current_user->roles[0];
}
}
return array(
'username' => $username,
// 'avatar' => $avatar_url,
'user_role' => $user_role,
);
}
}