felixarntz/site-icon-extended

View on GitHub
inc/WPSIE/App.php

Summary

Maintainability
A
1 hr
Test Coverage
<?php
/**
 * WPSIE\App class
 *
 * @package WPSIE
 * @author Felix Arntz <felix-arntz@leaves-and-love.net>
 * @since 0.1.0
 */

namespace WPSIE;

use LaL_WP_Plugin as Plugin;

if ( ! defined( 'ABSPATH' ) ) {
    die();
}

if ( ! class_exists( 'WPSIE\App' ) ) {
    /**
     * This class initializes the plugin.
     *
     * It also triggers the action and filter to hook into and contains all API functions of the plugin.
     *
     * @since 0.1.0
     */
    class App extends Plugin {

        /**
         * @since 0.1.0
         * @var array Holds the plugin data.
         */
        protected static $_args = array();

        /**
         * Class constructor.
         *
         * @since 0.1.0
         */
        protected function __construct( $args ) {
            parent::__construct( $args );
        }

        /**
         * The run() method.
         *
         * This will initialize the plugin if the Site Icon feature is available.
         *
         * @since 0.1.0
         */
        protected function run() {
            if ( ! function_exists( 'has_site_icon' ) ) {
                return;
            }

            $background_handler = BackgroundHandler::instance();
            $background_handler->add_hooks();

            $pinned_tab_icon_handler = PinnedTabIconHandler::instance();
            $pinned_tab_icon_handler->add_hooks();

            $xml_handler = XMLHandler::instance();
            $xml_handler->set_sizes( $this->get_sizes( 'browserconfig' ) );
            $xml_handler->add_hooks();

            $ico_handler = ICOHandler::instance();
            $ico_handler->set_sizes( $this->get_sizes( 'shortcut' ) );
            $ico_handler->add_hooks();

            add_filter( 'site_icon_image_sizes', array( $this, 'generate_image_sizes' ), 10, 1 );
            add_filter( 'site_icon_meta_tags', array( $this, 'generate_meta_tags' ), 0, 1 );
        }

        /**
         * Extends the image sizes array that defines in which sizes the site icon is stored.
         *
         * @since 0.1.0
         * @param array $image_sizes the original image sizes
         * @return array the extended image sizes
         */
        public function generate_image_sizes( $image_sizes = array() ) {
            $extended_sizes = $this->get_sizes();

            return array_unique( array_merge( $image_sizes, $extended_sizes ) );
        }

        /**
         * Generates all the meta tags for the site icon.
         *
         * This function overrides the WordPress Core meta tags.
         * In order not to cause conflicts with other plugins, this function should be hooked at the highest priority possible.
         *
         * @since 0.1.0
         * @param  array $meta_tags the original WordPress Core meta tags
         * @return array the extended meta tags generated by the plugin
         */
        public function generate_meta_tags( $meta_tags = array() ) {
            $meta_tags = array();

            $meta_tags = $this->generate_default_meta_tags( $meta_tags );
            $meta_tags = $this->generate_apple_touch_meta_tags( $meta_tags );
            $meta_tags = $this->generate_ms_application_meta_tags( $meta_tags );
            $meta_tags = $this->generate_browserconfig_meta_tags( $meta_tags );
            $meta_tags = $this->generate_pinned_tab_meta_tags( $meta_tags );
            $meta_tags = $this->generate_shortcut_meta_tags( $meta_tags );

            return $meta_tags;
        }

        /**
         * Gets all the required image sizes for the different icon types.
         *
         * If the $mode parameter is provided, the function returns sizes for a specific type.
         * Otherwise it will return all the sizes.
         *
         * @since 0.1.0
         * @param string $mode either 'default', 'shortcut', 'apple-touch', 'ms-application', 'browserconfig' or empty to get all sizes
         * @return array an array of integers for the icon sizes
         */
        protected function get_sizes( $mode = '' ) {
            $sizes = array(
                'default'            => array(
                    16,
                    32,
                    96,
                    160,
                    192,
                    196,
                ),
                'shortcut'            => array(
                    16,
                    32,
                    48,
                ),
                'apple-touch'        => array(
                    57,
                    60,
                    72,
                    76,
                    114,
                    120,
                    144,
                    152,
                    180,
                ),
                'ms-application'    => array(
                    270,
                ),
                'browserconfig'        => array(
                    70,
                    150,
                    310,
                ),
            );

            if ( ! empty( $mode ) ) {
                if ( isset( $sizes[ $mode ] ) ) {
                    return $sizes[ $mode ];
                }
                return array();
            }

            return array_merge( $sizes['default'], $sizes['shortcut'], $sizes['apple-touch'], $sizes['ms-application'], $sizes['browserconfig'] );
        }

        /**
         * Generates the default icon meta tags.
         *
         * @since 0.1.0
         * @param array $meta_tags the original meta tags
         * @return array the $meta_tags with the default icon meta tags added
         */
        protected function generate_default_meta_tags( $meta_tags = array() ) {
            $default_sizes = $this->get_sizes( 'default' );
            foreach ( $default_sizes as $size ) {
                $meta_tags[] = sprintf( '<link rel="icon" type="image/png" sizes="%1$s" href="%2$s">', sprintf( '%1$dx%1$d', $size ), esc_url( get_site_icon_url( $size ) ) );
            }

            return $meta_tags;
        }

        /**
         * Generates the Apple touch meta tags.
         *
         * @since 0.1.0
         * @param array $meta_tags the original meta tags
         * @return array the $meta_tags with the Apple touch meta tags added
         */
        protected function generate_apple_touch_meta_tags( $meta_tags = array() ) {
            $apple_touch_sizes = $this->get_sizes( 'apple-touch' );
            foreach ( $apple_touch_sizes as $size ) {
                $meta_tags[] = sprintf( '<link rel="apple-touch-icon-precomposed" sizes="%1$s" href="%2$s">', sprintf( '%1$dx%1$d', $size ), esc_url( get_site_icon_url( $size ) ) );
            }

            return $meta_tags;
        }

        /**
         * Generates the MS Application icon meta tags.
         *
         * @since 0.1.0
         * @param array $meta_tags the original meta tags
         * @return array the $meta_tags with the MS Application icon meta tags added
         */
        protected function generate_ms_application_meta_tags( $meta_tags = array() ) {
            $background_color = BackgroundHandler::instance()->get_background_color();
            if ( $background_color ) {
                $meta_tags[] = sprintf( '<meta name="msapplication-TileColor" content="%s">', esc_attr( '#' . ltrim( $background_color, '#' ) ) );
            }

            $ms_application_sizes = $this->get_sizes( 'ms-application' );
            foreach ( $ms_application_sizes as $size ) {
                $meta_tags[] = sprintf( '<meta name="msapplication-TileImage" content="%s">', esc_url( get_site_icon_url( $size ) ) );
            }

            return $meta_tags;
        }

        /**
         * Generates the browserconfig.xml meta tags.
         *
         * @since 0.1.0
         * @param array $meta_tags the original meta tags
         * @return array the $meta_tags with the browserconfig.xml meta tags added
         */
        protected function generate_browserconfig_meta_tags( $meta_tags = array() ) {
            $browserconfig_url = XMLHandler::instance()->get_browserconfig_url();
            if ( $browserconfig_url ) {
                $meta_tags[] = sprintf( '<meta name="msapplication-config" content="%s">', esc_url( $browserconfig_url ) );
            }

            return $meta_tags;
        }

        /**
         * Generates the Pinned Tab Icon meta tags.
         *
         * @since 0.2.0
         * @param array $meta_tags the original meta tags
         * @return array the $meta_tags with the Pinned Tab Icon meta tags added
         */
        protected function generate_pinned_tab_meta_tags( $meta_tags = array() ) {
            $pinned_tab_icon_url = PinnedTabIconHandler::instance()->get_svg_url();
            if ( $pinned_tab_icon_url ) {
                $pinned_tab_icon_color = PinnedTabIconHandler::instance()->get_color();
                $meta_tags[] = sprintf( '<link rel="mask-icon" href="%1$s" color="%2$s">', esc_url( $pinned_tab_icon_url ), esc_attr( $pinned_tab_icon_color ) );
            }

            return $meta_tags;
        }

        /**
         * Generates the shortcut icon meta tags.
         *
         * @since 0.1.0
         * @param array $meta_tags the original meta tags
         * @return array the $meta_tags with the shortcut icon meta tags added
         */
        protected function generate_shortcut_meta_tags( $meta_tags = array() ) {
            $ico_url = ICOHandler::instance()->get_ico_url();
            if ( $ico_url ) {
                $meta_tags[] = sprintf( '<link rel="shortcut icon" href="%s">', esc_url( $ico_url ) );
            }

            return $meta_tags;
        }

        /**
         * Handles plugin activation.
         *
         * @since 0.1.0
         */
        public static function activate() {
            XMLHandler::instance()->add_rewrite_rule();
            add_action( 'shutdown', 'flush_rewrite_rules' );

            return true;
        }

        /**
         * Handles plugin deactivation.
         *
         * @since 0.1.0
         */
        public static function deactivate() {
            add_action( 'shutdown', 'flush_rewrite_rules' );

            return true;
        }

        /**
         * Handles plugin uninstallation.
         *
         * @since 0.1.0
         */
        public static function uninstall() {
            delete_option( 'wpsie_background_color' );
            delete_option( 'wpsie_pinned_tab_icon_url' );
            delete_option( 'wpsie_pinned_tab_icon_color' );
            delete_post_meta_by_key( 'wpsie_ico_id' );
            delete_transient( 'wpsie_check_ico_file' );

            return true;
        }
    }
}