<?php
/**
 * Custom template tags for this theme.
 *
 * Eventually, some of the functionality here could be replaced by core features.
 *
 * @package enjoystyle
 */

/**
 * Adds a meta box to the post editing screen
 */
function enjoystyle_featured_meta() {
    add_meta_box( 'enjoystyle_meta', __( 'Featured Post', 'enjoystyle' ), 'enjoystyle_meta_callback', 'post', 'normal', 'high' );
}
add_action( 'add_meta_boxes', 'enjoystyle_featured_meta' );
 
/**
 * Outputs the content of the meta box
 */
 
function enjoystyle_meta_callback( $post ) {
    wp_nonce_field( basename( __FILE__ ), 'enjoystyle_nonce' );
    $enjoystyle_stored_meta = get_post_meta( $post->ID );
    ?>
 
 <p>
    <span class="enjoystyle-row-title"><?php esc_html_e( 'Display post in the homepage slider?', 'enjoystyle' )?></span>
    <div class="enjoystyle-row-content">
        <label for="enjoystyle-featured">
            <input type="checkbox" name="enjoystyle-featured" id="enjoystyle-featured" value="yes" <?php if ( isset ( $enjoystyle_stored_meta['enjoystyle-featured'] ) ) checked( $enjoystyle_stored_meta['enjoystyle-featured'][0], 'yes' ); ?> />
            <?php esc_html_e( 'Featured Post', 'enjoystyle' )?>
        </label>
 
    </div>
</p>   
 
    <?php
}
 
/**
 * Saves the custom meta input
 */
function enjoystyle_meta_save( $post_id ) {
 
    // Checks save status - overcome autosave, etc.
    $is_autosave = wp_is_post_autosave( $post_id );
    $is_revision = wp_is_post_revision( $post_id );
    $is_valid_nonce = ( isset( $_POST[ 'enjoystyle_nonce' ] ) && wp_verify_nonce( sanitize_text_field( wp_unslash($_POST[ 'enjoystyle_nonce' ], basename( __FILE__ ) ) ) ) ) ? 'true' : 'false';
 
    // Exits script depending on save status
    if ( $is_autosave || $is_revision || !$is_valid_nonce ) {
        return;
    }

    //Don't update on Quick Edit
    if (defined('DOING_AJAX') ) {
        return $post_id;
    }    

    //Don't update on Bulk Edit
    if (isset($_REQUEST['bulk_edit'])) {
        return;
    }
 
    // Checks for input and saves - save checked as yes and unchecked at no
    if( isset( $_POST[ 'enjoystyle-featured' ] ) ) {
        update_post_meta( $post_id, 'enjoystyle-featured', 'yes' );
    } else {
        update_post_meta( $post_id, 'enjoystyle-featured', 'no' );
    }
 
}
add_action( 'save_post', 'enjoystyle_meta_save' );

/**
 * Add admin column for featured posts
 */
add_filter('manage_post_posts_columns', function($columns) {
    return array_merge($columns, ['enjoystyle-featured' => __('Featured', 'enjoystyle')]);
}); 

add_action('manage_post_posts_custom_column', function($column_key, $post_id) {
    if ($column_key == 'enjoystyle-featured') {
        $enjoystyle_featured = get_post_meta($post_id, 'enjoystyle-featured', 'yes');
        if ($enjoystyle_featured == 'yes') {
            echo '<span style="color:green;"><span class="dashicons dashicons-saved"></span></span>';
        } else {
            echo '';
        }
    }
}, 10, 2);

add_filter('manage_edit-post_sortable_columns', function($columns) {
    $columns['enjoystyle-featured'] = 'enjoystyle-featured';
    return $columns;
});

add_action('pre_get_posts', function($query) {
    if (!is_admin()) {
        return;
    }
 
    $orderby = $query->get('orderby');
    if ($orderby == 'enjoystyle-featured') {
        $query->set('meta_key', 'enjoystyle-featured');
        $query->set('meta_value', 'yes');
        $query->set('orderby', 'date');
        $query->set('order', 'DESC');
    }
});

/**
 * Search Filter 
 */
if ( ! function_exists( 'enjoystyle_search_filter' ) && ( ! is_admin() ) ) :

function enjoystyle_search_filter($query) {
	if ($query->is_search) {
		$query->set('post_type', 'post');
	}
	return $query;
}

add_filter('pre_get_posts','enjoystyle_search_filter');

endif;

/**
 * Filter the except length to 27 characters.
 *
 * @param int $length Excerpt length.
 * @return int (Maybe) modified excerpt length.
 */
if ( ! function_exists( 'enjoystyle_custom_excerpt_length' ) ) :

function enjoystyle_custom_excerpt_length( $length ) {
    if ( is_admin() ) {
        return $length;
    } else {
       return '27'; 
    }
}
add_filter( 'excerpt_length', 'enjoystyle_custom_excerpt_length', 999 );

endif;

/**
 * Customize excerpt more.
 */
if ( ! function_exists( 'enjoystyle_excerpt_more' ) ) :

function enjoystyle_excerpt_more( $more ) {
    if ( is_admin() ) {
        return $more;
    } else {
        return '... ';
    }
}
add_filter( 'excerpt_more', 'enjoystyle_excerpt_more' );

endif;

/**
 * Display the first (single) category of post.
 */
if ( ! function_exists( 'enjoystyle_first_category' ) ) :
function enjoystyle_first_category() {
    $category = get_the_category();
    if ($category) {
      echo '<a href="' . esc_url( get_category_link( $category[0]->term_id ) ) . '">' . esc_html( $category[0]->name ) .'</a> ';
    }    
}
endif;

/**
 * Flush out the transients used in enjoystyle_categorized_blog.
 */
if ( ! function_exists( 'enjoystyle_category_transient_flusher' ) ) :

function enjoystyle_category_transient_flusher() {
    if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
        return;
    }
    // Like, beat it. Dig?
    delete_transient( 'enjoystyle_categories' );
}
add_action( 'edit_category', 'enjoystyle_category_transient_flusher' );
add_action( 'save_post',     'enjoystyle_category_transient_flusher' );

endif;

/**
 * Disable specified widgets.
 */
if ( ! function_exists( 'enjoystyle_disable_specified_widgets' ) ) :

function enjoystyle_disable_specified_widgets( $sidebars_widgets ) {

    if ( isset($sidebars_widgets['header-ad']) ) {
        if ( is_array($sidebars_widgets['header-ad']) ) {
               foreach($sidebars_widgets['header-ad'] as $i => $widget) {
                    if( (strpos($widget, 'wpenjoy-') === false) ) {
                       unset($sidebars_widgets['header-ad'][$i]);
                    }
               }
        }     
    }

    if ( isset($sidebars_widgets['content-ad']) ) {
        if ( is_array($sidebars_widgets['content-ad']) ) {
               foreach($sidebars_widgets['content-ad'] as $i => $widget) {
                    if( (strpos($widget, 'wpenjoy-') === false) ) {
                       unset($sidebars_widgets['content-ad'][$i]);
                    }
               }
        }
    }                

    return $sidebars_widgets;
}
add_filter( 'sidebars_widgets', 'enjoystyle_disable_specified_widgets' );

endif;

/*
 * Limit Tags Count 
 */
//Register tag cloud filter callback
add_filter('widget_tag_cloud_args', 'enjoystyle_tag_widget_limit');
 
//Limit number of tags inside widget
function enjoystyle_tag_widget_limit($args){
 
//Check if taxonomy option inside widget is set to tags
if(isset($args['taxonomy']) && $args['taxonomy'] == 'post_tag'){
  $args['number'] = '30'; //Limit number of tags
}
 
return $args;
}

/**
 * Fix skip link focus in IE11.
 *
 * This does not enqueue the script because it is tiny and because it is only for IE11,
 * thus it does not warrant having an entire dedicated blocking script being loaded.
 *
 * @link https://git.io/vWdr2
 */
function enjoystyle_skip_link_focus_fix() {
    // The following is minified via `terser --compress --mangle -- js/skip-link-focus-fix.js`.
    ?>
    <script>
    /(trident|msie)/i.test(navigator.userAgent)&&document.getElementById&&window.addEventListener&&window.addEventListener("hashchange",function(){var t,e=location.hash.substring(1);/^[A-z0-9_-]+$/.test(e)&&(t=document.getElementById(e))&&(/^(?:a|select|input|button|textarea)$/i.test(t.tagName)||(t.tabIndex=-1),t.focus())},!1);
    </script>
    <?php
}
add_action( 'wp_print_footer_scripts', 'enjoystyle_skip_link_focus_fix' );

/**
 * Twenty Twenty SVG Icon helper functions
 *
 * @package enjoystyle
 * @subpackage enjoystyle
 * @since EnjoyStyle 1.0
 */

if ( ! function_exists( 'enjoystyle_the_theme_svg' ) ) {
    /**
     * Output and Get Theme SVG.
     * Output and get the SVG markup for an icon in the EnjoyStyle_SVG_Icons class.
     *
     * @param string $svg_name The name of the icon.
     * @param string $group The group the icon belongs to.
     * @param string $color Color code.
     */
    function enjoystyle_the_theme_svg( $svg_name, $group = 'ui', $color = '' ) {
        echo enjoystyle_get_theme_svg( $svg_name, $group, $color ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Escaped in enjoystyle_get_theme_svg().
    }
}

if ( ! function_exists( 'enjoystyle_get_theme_svg' ) ) {

    /**
     * Get information about the SVG icon.
     *
     * @param string $svg_name The name of the icon.
     * @param string $group The group the icon belongs to.
     * @param string $color Color code.
     */
    function enjoystyle_get_theme_svg( $svg_name, $group = 'ui', $color = '' ) {

        // Make sure that only our allowed tags and attributes are included.
        $svg = wp_kses(
            EnjoyStyle_SVG_Icons::get_svg( $svg_name, $group, $color ),
            array(
                'svg'     => array(
                    'class'       => true,
                    'xmlns'       => true,
                    'width'       => true,
                    'height'      => true,
                    'viewbox'     => true,
                    'aria-hidden' => true,
                    'role'        => true,
                    'focusable'   => true,
                ),
                'path'    => array(
                    'fill'      => true,
                    'fill-rule' => true,
                    'd'         => true,
                    'transform' => true,
                ),
                'polygon' => array(
                    'fill'      => true,
                    'fill-rule' => true,
                    'points'    => true,
                    'transform' => true,
                    'focusable' => true,
                ),
            )
        );

        if ( ! $svg ) {
            return false;
        }
        return $svg;
    }
}


/**
 * Miscellaneous
 */

/**
 * Toggles animation duration in milliseconds.
 *
 * @return int Duration in milliseconds
 */
function enjoystyle_toggle_duration() {
    /**
     * Filters the animation duration/speed used usually for submenu toggles.
     *
     * @since EnjoyStyle 1.0
     *
     * @param int $duration Duration in milliseconds.
     */
    $duration = apply_filters( 'enjoystyle_toggle_duration', 250 );

    return $duration;
}


/**
 * Menus
 */

/**
 * Filters classes of wp_list_pages items to match menu items.
 *
 * Filter the class applied to wp_list_pages() items with children to match the menu class, to simplify.
 * styling of sub levels in the fallback. Only applied if the match_menu_classes argument is set.
 *
 * @param string[] $css_class    An array of CSS classes to be applied to each list item.
 * @param WP_Post  $page         Page data object.
 * @param int      $depth        Depth of page, used for padding.
 * @param array    $args         An array of arguments.
 * @param int      $current_page ID of the current page.
 * @return array CSS class names.
 */
function enjoystyle_filter_wp_list_pages_item_classes( $css_class, $page, $depth, $args, $current_page ) {

    // Only apply to wp_list_pages() calls with match_menu_classes set to true.
    $match_menu_classes = isset( $args['match_menu_classes'] );

    if ( ! $match_menu_classes ) {
        return $css_class;
    }

    // Add current menu item class.
    if ( in_array( 'current_page_item', $css_class, true ) ) {
        $css_class[] = 'current-menu-item';
    }

    // Add menu item has children class.
    if ( in_array( 'page_item_has_children', $css_class, true ) ) {
        $css_class[] = 'menu-item-has-children';
    }

    return $css_class;

}

add_filter( 'enjoystyle_page_css_class', 'enjoystyle_filter_wp_list_pages_item_classes', 10, 5 );

/**
 * Adds a Sub Nav Toggle to the Expanded Menu and Mobile Menu.
 *
 * @param stdClass $args  An object of wp_nav_menu() arguments.
 * @param WP_Post  $item  Menu item data object.
 * @param int      $depth Depth of menu item. Used for padding.
 * @return stdClass An object of wp_nav_menu() arguments.
 */
function enjoystyle_add_sub_toggles_to_main_menu( $args, $item, $depth ) {

    // Add sub menu toggles to the Expanded Menu with toggles.
    if ( isset( $args->show_toggles ) && $args->show_toggles ) {

        // Wrap the menu item link contents in a div, used for positioning.
        $args->before = '<div class="ancestor-wrapper">';
        $args->after  = '';

        // Add a toggle to items with children.
        if ( in_array( 'menu-item-has-children', $item->classes, true ) ) {

            $toggle_target_string = '.menu-modal .menu-item-' . $item->ID . ' > .sub-menu';
            $toggle_duration      = enjoystyle_toggle_duration();

            // Add the sub menu toggle.
            $args->after .= '<button class="toggle sub-menu-toggle fill-children-current-color" data-toggle-target="' . $toggle_target_string . '" data-toggle-type="slidetoggle" data-toggle-duration="' . absint( $toggle_duration ) . '" aria-expanded="false"><span class="screen-reader-text">' . __( 'Show sub menu', 'enjoystyle' ) . '</span>' . enjoystyle_get_theme_svg( 'chevron-down' ) . '</button>';

        }

        // Close the wrapper.
        $args->after .= '</div><!-- .ancestor-wrapper -->';

        // Add sub menu icons to the primary menu without toggles.
    } elseif ( 'primary' === $args->theme_location ) {
        if ( in_array( 'menu-item-has-children', $item->classes, true ) ) {
            $args->after = '<span class="icon"></span>';
        } else {
            $args->after = '';
        }
    }

    return $args;

}

add_filter( 'nav_menu_item_args', 'enjoystyle_add_sub_toggles_to_main_menu', 10, 3 );

/*
 * Customize archive title
 */
add_filter( 'get_the_archive_title', function ($title) {    
    if ( is_category() ) {    
            $title = single_cat_title( '', false );    
        } elseif ( is_tag() ) {    
            $title = single_tag_title( '', false );    
        } elseif ( is_author() ) {    
            $title = '<span class="vcard">' . get_the_author() . '</span>' ;    
        } elseif ( is_tax() ) {
            $title = single_term_title( '', false );
        } elseif (is_post_type_archive()) {
            $title = post_type_archive_title( '', false );
        }
    return $title;    
});

/*
 * Featured Image Before Content
 */   
//add_filter( 'the_content', 'enjoystyle_featured_image_before_content' ); 

function enjoystyle_featured_image_before_content( $content ) {
    $post_id = '';
    if ( is_singular('post') && has_post_thumbnail()) {
        $featured_image = get_the_post_thumbnail( $post_id, 'large', array( 'class' => 'alignnone' ) );
        $content = '<p>' . $featured_image . '</p>' . $content;
    }
    return $content;

}

/**
 * Displays SVG icons in social links menu.
 *
 * @param string   $item_output The menu item's starting HTML output.
 * @param WP_Post  $item        Menu item data object.
 * @param int      $depth       Depth of the menu. Used for padding.
 * @param stdClass $args        An object of wp_nav_menu() arguments.
 * @return string The menu item output with social icon.
 */
function enjoystyle_nav_menu_social_icons( $item_output, $item, $depth, $args ) {
    // Change SVG icon inside social links menu if there is supported URL.
    if ( 'social' === $args->theme_location ) {
        $svg = enjoystyle_SVG_Icons::get_social_link_svg( $item->url );
        if ( empty( $svg ) ) {
            $svg = enjoystyle_get_theme_svg( 'link' );
        }
        $item_output = str_replace( $args->link_after, '</span>' . $svg, $item_output );
    }

    return $item_output;
}

add_filter( 'walker_nav_menu_start_el', 'enjoystyle_nav_menu_social_icons', 10, 4 );

/*
 * Remove parentheses from categories widget
 */
function enjoystyle_categories_postcount_filter ($variable) {
   $variable = str_replace('(', '<span class="post-count"> ', $variable);
   $variable = str_replace(')', ' </span>', $variable);
   return $variable;
}
add_filter('wp_list_categories','enjoystyle_categories_postcount_filter');

/* Admin Style */
function enjoystyle_admin_init() {
    if ( is_admin() ) {
        wp_enqueue_style("enjoystyle-admin-css", get_template_directory_uri() . "/assets/css/admin-style.css", false, "1.0", "all");
    }
}
add_action( 'admin_enqueue_scripts', 'enjoystyle_admin_init' );

/**
 * Pro upgrade notice
 */
function enjoystyle_pro_upgrade_admin_notice() {

    if ( ! current_user_can( 'manage_options' ) ) {
        return;
    }
    ?>
    <div class="notice notice-info is-dismissible enjoystyle-pro-notice enjoystyle-pro-animate">
        <div class="">

            <div class="enjoystyle-pro-header">
                <span class="dashicons dashicons-star-filled"></span>
                <strong class="enjoystyle-pro-title">
                    <?php esc_html_e( 'Upgrade to EnjoyStyle Pro', 'enjoystyle' ); ?>
                </strong>
            </div>
            <p class="enjoystyle-pro-desc">
                <?php esc_html_e( 'You are currently using the free version of the EnjoyStyle theme. Upgrade to the Pro version to unlock additional features:', 'enjoystyle' ); ?>
            </p>
            <ul class="enjoystyle-pro-features">
                <li><?php esc_html_e( 'Theme Options Panel', 'enjoystyle' ); ?></li>
                <li><?php esc_html_e( 'One Click Demo Import', 'enjoystyle' ); ?></li>
                <li><?php esc_html_e( '1000+ Google Fonts', 'enjoystyle' ); ?></li>
                <li><?php esc_html_e( 'Unlimited Color Schemes', 'enjoystyle' ); ?></li>
                <li><?php esc_html_e( 'Featured Content Options', 'enjoystyle' ); ?></li>
                <li><?php esc_html_e( 'Archive (Post List) Options', 'enjoystyle' ); ?></li>                
                <li><?php esc_html_e( 'Ad Position Management', 'enjoystyle' ); ?></li>
                <li><?php esc_html_e( 'Social Share Icons for Posts', 'enjoystyle' ); ?></li>
                <li><?php esc_html_e( 'Priority Theme Support and More', 'enjoystyle' ); ?></li>                    
            </ul>

            <div class="cta-button">
                <?php 
                    $theme = wp_get_theme();
                ?>
                <a href="<?php echo esc_url( $theme->get( 'AuthorURI' ) . '/themes/enjoystyle-pro' ); ?>"
                   class="button button-primary enjoystyle-pro-btn"
                   target="_blank"
                   rel="noopener noreferrer">
                    <?php esc_html_e( 'Upgrade to EnjoyStyle Pro', 'enjoystyle' ); ?>
                </a>
            </div>

        </div>
    </div>
    <?php
}
add_action( 'admin_notices', 'enjoystyle_pro_upgrade_admin_notice' );

add_action( 'admin_enqueue_scripts', function () {
    wp_add_inline_style(
        'wp-admin',
        '
        .enjoystyle-pro-notice {
            padding: 0;
            border-left: none;
            background: transparent;
        }

        .enjoystyle-pro-animate {
            position: relative;
            padding: 14px 18px 16px;
            background: #fffaf3;
            border-radius: 4px;
        }

        .enjoystyle-pro-animate::before {
            content: "";
            position: absolute;
            inset: 0;
            border-left: 6px solid #ff9800;
            animation: enjoystyleBorderPulse 2.2s ease-in-out infinite;
        }

        .enjoystyle-pro-header {
            display: flex;
            align-items: center;
            gap: 8px;
            margin-bottom: 6px;
            position: relative;
            z-index: 1;
        }

        .enjoystyle-pro-header .dashicons {
            color: #ff9800;
            font-size: 22px;
        }

        .enjoystyle-pro-title {
            font-size: 14px;
        }

        /* Clean list styling */
        .enjoystyle-pro-features {
            list-style: none;
            margin: 0 0 10px;
            padding: 0;
            position: relative;
            z-index: 1;
        }

        .enjoystyle-pro-features li {
            font-size: 13px;
            color: #555;
            padding-left: 20px;
            margin: 2px 0;
            position: relative;
        }

        .enjoystyle-pro-features li::before {
            content: "\f147"; /* dashicons-yes */
            font-family: dashicons;
            position: absolute;
            left: 0;
            top: 0;
            color: #46b450;
            font-size: 16px;
        }

        .cta-button {
            position: relative;
            z-index: 1;
        }

        @keyframes enjoystyleBorderPulse {
            0%   { box-shadow: 0 0 0 0 rgba(255,152,0,.35); }
            70%  { box-shadow: 0 0 0 10px rgba(255,152,0,0); }
            100% { box-shadow: 0 0 0 0 rgba(255,152,0,0); }
        }
        '
    );
});
