'', 'post_id' => 0, ), $atts, 'ajwa_widget' ); $settings = ajwa_get_settings(); $position = ! empty( $atts['position'] ) ? ( 'left' === $atts['position'] ? 'left' : 'right' ) : ( $settings['widget_position'] ?? 'right' ); $post_id = absint( $atts['post_id'] ); if ( ! $post_id ) { global $post; $post_id = $post ? $post->ID : 0; } if ( ! $post_id ) { return ''; } $page_url = get_permalink( $post_id ); $md_url = AJWA_Frontend::get_md_url( $post_id ); $actions = $settings['actions'] ?? array(); usort( $actions, fn( $a, $b ) => ( $a['order'] ?? 0 ) <=> ( $b['order'] ?? 0 ) ); $actions = array_filter( $actions, fn( $a ) => ! empty( $a['enabled'] ) ); if ( empty( $actions ) ) { return ''; } // Enqueue assets if not already done. if ( ! wp_script_is( 'ajwa-widget', 'enqueued' ) ) { wp_enqueue_style( 'ajwa-widget', AJWA_PLUGIN_URL . 'public/css/widget.css', array(), AJWA_VERSION ); wp_enqueue_script( 'ajwa-widget', AJWA_PLUGIN_URL . 'public/js/widget.js', array( 'jquery' ), AJWA_VERSION, true ); wp_localize_script( 'ajwa-widget', 'ajwaWidget', array( 'ajaxurl' => admin_url( 'admin-ajax.php' ), 'nonce' => wp_create_nonce( 'ajwa_frontend_nonce' ), 'post_id' => $post_id, 'page_url' => $page_url, 'md_url' => $md_url, 'strings' => array( 'copying' => esc_html__( 'Copying…', 'ajw-ai-markdown-page-widget' ), 'copied' => esc_html__( 'Copied!', 'ajw-ai-markdown-page-widget' ), 'error' => esc_html__( 'Error. Try again.', 'ajw-ai-markdown-page-widget' ), ), ) ); } $pos_class = ( 'left' === $position ) ? 'ajwa-widget--left' : 'ajwa-widget--right'; $out = '
'; return $out; } } /** * WordPress sidebar widget. * * @package AJW_AI_Markdown_Page_Widget */ class AJWA_Widget extends WP_Widget { /** * Constructor. */ public function __construct() { parent::__construct( 'ajwa_widget', esc_html__( 'AJW AI Widget', 'ajw-ai-markdown-page-widget' ), array( 'description' => esc_html__( 'Displays the AJW AI Markdown widget actions for the current page.', 'ajw-ai-markdown-page-widget' ), ) ); } /** * Front-end display. * * @param array $args Widget args. * @param array $instance Saved values. */ public function widget( $args, $instance ) { $position = ! empty( $instance['position'] ) ? sanitize_key( $instance['position'] ) : ''; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- shortcode output is escaped inside echo do_shortcode( '[ajwa_widget position="' . esc_attr( $position ) . '"]' ); } /** * Back-end widget form. * * @param array $instance Previously saved values. */ public function form( $instance ) { $position = ! empty( $instance['position'] ) ? $instance['position'] : ''; ?>