TeaThemeOptions/TeaThemeOptions

View on GitHub
src/Core/Hook/HookFrontend.php

Summary

Maintainability
C
7 hrs
Test Coverage
<?php

namespace crewstyle\OlympusZeus\Core\Hook;

use crewstyle\OlympusZeus\OlympusZeus;

/**
 * Gets its own hooks.
 *
 * @package Olympus Zeus
 * @subpackage Core\Hook\HookFrontend
 * @author Achraf Chouk <achrafchouk@gmail.com>
 * @since 4.0.0
 *
 */

class HookFrontend
{
    /**
     * Constructor.
     *
     * @since 4.0.0
     */
    public function __construct()
    {
        //Admin panel
        if (OLZ_ISADMIN) {
            return;
        }
    }

    /**
     * Build admin hooks.
     *
     * @since 4.0.0
     */
    public function makeHooks()
    {
        //Admin panel
        if (OLZ_ISADMIN) {
            return;
        }

        //Get configs
        $hooks = OlympusZeus::getConfigs('frontendhooks');

        //Auto-generated tags
        if (isset($hooks['generated']) && $hooks['generated']) {
            //Remove admin bar since v3.1
            add_filter('show_admin_bar', '__return_false');
            remove_action('init', 'wp_admin_bar_init');

            //Remove automatic feed links since WP v.3.x
            remove_theme_support('automatic-feed-links');

            //Remove feed links
            //<link rel="alternate" type="application/rss+xml" title="__SITE_NAME__ &raquo; Comments Feed" href="__SITE_URL__/comments/feed/" />
            remove_action('wp_head', 'feed_links', 2);

            //Remove extra feed links, such as categories, tags, etc.
            //<link rel="alternate" type="application/rss+xml" title="__SITE_NAME__ &raquo; Comments Feed" href="__SITE_URL__/category/__CAT__/feed/" />
            remove_action('wp_head', 'feed_links_extra', 3);

            //Remove WORDPRESS version.
            //<meta name="generator" content="WordPress __WP_VERSION__" />
            remove_action('wp_head', 'wp_generator');

            //Link to adjacent posts.
            //<link rel="prev" title="adjacent_posts_rel_link" href="__SITE_URL__" />
            remove_action('wp_head', 'adjacent_posts_rel_link_wp_head', 10, 0);

            //Link to blog index.
            //<link rel="index" title="__SITE_NAME__" href="__SITE_URL__" />
            remove_action('wp_head', 'index_rel_link');

            //Really Simple Discovery support.
            //<link rel="EditURI" type="application/rsd+xml" title="RSD" href="__SITE_URL__" />
            remove_action('wp_head', 'rsd_link');

            //Remove Shortlink Link Rel Hook
            remove_action('wp_head', 'wp_shortlink_wp_head', 10, 0);

            //Windows Live Writter support.
            //<link rel="wlwmanifest" type="application/wlwmanifest+xml" href="__SITE_URL__" />
            remove_action('wp_head', 'wlwmanifest_link');

            //Others
            remove_action('wp_head', 'parent_post_rel_link', 10, 0);
            remove_action('wp_head', 'start_post_rel_link', 10, 0);

            //Remove i18n styles
            remove_action('wp_head', 'wp_dlmp_l10n_style');

            //Shortcodes
            remove_filter('the_content', 'wpautop');
            remove_filter('the_content', 'wptexturize');
        }

        //Hooks
        if (isset($hooks['bodyclass']) && $hooks['bodyclass']) {
            add_filter('body_class',        array(&$this, 'hookCleanBodyClass'), 10, 2);
        }

        if (isset($hooks['emojicons']) && $hooks['emojicons']) {
            add_action('init',              array(&$this, 'hookDisableWPEmojicons'));
        }

        if (isset($hooks['version']) && $hooks['version']) {
            add_filter('the_generator',     array(&$this, 'hookRemoveVersion'));
        }

        if (isset($hooks['shortcodeformatter']) && $hooks['shortcodeformatter']) {
            add_filter('the_content',       array(&$this, 'hookShortcodeFormatter'), 99);
        }
    }

    /**
     * Remove unecessary details in body_class.
     *
     * @param string $classes Default classes for `body_class()`
     * @param string $class Other classes for `body_class()`
     * @uses get_query_var() Get a special value from query.
     * @uses get_user_by() Get user data for an unknown user.
     * @uses get_userdata() Get user data for a known user.
     * @uses is_author() To know if current page is an author's page.
     * @uses sanitize_html_class() 
     * @uses get_queried_object() 
     * @uses get_queried_object_id() 
     *
     * @since 3.3.0
     */
    public function hookCleanBodyClass($classes, $class)
    {
        //Remove unecessary CSS classes generated by WORDPRESS on authors pages
        if (isset($classes['author'])) {
            //Get author
            if (!$name = get_query_var('author_name')) {
                $author = get_userdata(get_query_var('author'));
            }
            else {
                $author = get_user_by('slug', $name);
            }

            //Get data to remove
            $removed[] = 'author-'.$author->ID;
            $removed[] = 'author-'.$author->user_nicename;

            //Remove classes
            $classes = array_diff($classes, $removed);
        }

        //Remove unecessary CSS classes generated by WORDPRESS on single pages
        if (isset($classes['single'])) {
            //Get post details
            $post = get_queried_object();
            $pid = get_queried_object_id();

            //Check post type
            if (isset($post->post_type)) {
                $removed[] = 'single-'.sanitize_html_class($post->post_type, $pid);
                $removed[] = 'postid-'.$pid;

                //Remove classes
                $classes = array_diff($classes, $removed);
            }
        }

        //Remove unecessary CSS classes generated by WORDPRESS on categories / tags / tax
        if (isset($classes['category']) || isset($classes['tag']) || isset($classes['tax'])) {
            //Get object details
            $obj = get_queried_object();

            //Check object ID
            if (isset($obj->term_id)) {
                //Get class
                $obj_class = sanitize_html_class($obj->slug, $obj->term_id);
                $obj_class = is_numeric($obj_class) || !trim($obj_class, '-') ? 
                    $obj->term_id : 
                    $obj_class;

                //Remove details
                if (isset($classes['category'])) {
                    $removed[] = 'category-'.$obj_class;
                    $removed[] = 'category-'.$obj->term_id;
                }
                else if (isset($classes['tag'])) {
                    $removed[] = 'tag-'.$obj_class;
                    $removed[] = 'tag-'.$obj->term_id;
                }
                else if (isset($classes['tax'])) {
                    $removed[] = 'tax-'.sanitize_html_class($obj->taxonomy);
                    $removed[] = 'term-'.$obj_class;
                    $removed[] = 'term-'.$obj->term_id;
                }

                //Remove classes
                $classes = array_diff($classes, $removed);
            }
        }

        //Return all classes
        return array_merge($classes, (array)$class);
    }

    /**
     * Disable emojicons introduced with WP 4.2 in frontend panel.
     *
     * @uses remove_action()
     * @uses add_filter()
     *
     * @since 3.3.0
     */
    public function hookDisableWPEmojicons()
    {
        //All actions related to emojis
        remove_action('wp_head', 'print_emoji_detection_script', 7);
        remove_action('wp_print_styles', 'print_emoji_styles');
        remove_filter('wp_mail', 'wp_staticize_emoji_for_email');
        remove_filter('the_content_feed', 'wp_staticize_emoji');
        remove_filter('comment_text_rss', 'wp_staticize_emoji');
    }

    /**
     * Remove generated WP version.
     *
     * @since 3.3.0
     */
    public function hookRemoveVersion()
    {
        return '';
    }

    /**
     * Delete WP auto-formatting.
     *
     * @uses wptexturize()
     * @uses wpautop() To format content
     *
     * @since 3.3.0
     */
    public function hookShortcodeFormatter($content)
    {
        $new_content = '';
        $pattern_full = '{(\[raw\].*?\[/raw\])}is';
        $pattern_contents = '{\[raw\](.*?)\[/raw\]}is';
        $pieces = preg_split($pattern_full, $content, -1, PREG_SPLIT_DELIM_CAPTURE);

        //Make the magic
        foreach ($pieces as $piece) {
            if (preg_match($pattern_contents, $piece, $matches)) {
                $new_content .= $matches[1];
            }
            else {
                $new_content .= wptexturize(wpautop($piece));
            }
        }

        return $new_content;
    }
}