Theme.php 3.54 KB
Newer Older
Pham Huy committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134
<?php
/**
 * Represents a bundle of type "theme"
 */
class Loco_package_Theme extends Loco_package_Bundle {

    /**
     * @var Loco_package_Theme
     */
    private $parent;


    /**
     * {@inheritdoc}
     */
    public function getSystemTargets(){
        return array ( 
            trailingslashit( loco_constant('LOCO_LANG_DIR') ).'themes',
            trailingslashit( loco_constant('WP_LANG_DIR') ).'themes',
        );
    }


    /**
     * {@inheritdoc}
     */
    public function isTheme(){
        return true;
    }


    /**
     * {@inheritdoc}
     */
    public function getType(){
        return 'Theme';
    }


    /**
     * {@inheritdoc}
     */
    public function getHeaderInfo(){
        $root = dirname( $this->getDirectoryPath() );
        $theme = new WP_Theme( $this->getSlug(), $root );
        return new Loco_package_Header( $theme );
    }


    /**
     * {@inheritdoc}
     */
    public function getMetaTranslatable(){
        return array (
            'Name'        => 'Name of the theme',
            'Description' => 'Description of the theme',
            'ThemeURI'    => 'URI of the theme',
            'Author'      => 'Author of the theme',
            'AuthorURI'   => 'Author URI of the theme',
            // 'Tags'        => 'Tags of the theme',
        );
    }


    /**
     * Get parent bundle if theme is a child
     * @return Loco_package_Theme
     */
    public function getParent(){
        return $this->parent;
    }


    /**
     * Create theme bundle definition from WordPress theme handle 
     * 
     * @param string short name of theme, e.g. "twentyfifteen"
     * @return Loco_package_Plugin
     */
    public static function create( $slug, $root = null ){
        return self::createFromTheme( wp_get_theme( $slug, $root ) );
    }



    /**
     * Create theme bundle definition from WordPress theme data 
     */
    public static function createFromTheme( WP_Theme $theme ){
        $slug = $theme->get_stylesheet();
        $base = $theme->get_stylesheet_directory();
        $name = $theme->get('Name') or $name = $slug;
        if( ! $theme->exists() ){
            throw new Loco_error_Exception('Theme not found: '.$name );
        }

        $bundle = new Loco_package_Theme( $slug, $name );
        
        // ideally theme has declared its TextDomain
        $domain = $theme->get('TextDomain') or
        // if not, we can see if the Domain listener has picked it up 
        $domain = Loco_package_Listener::singleton()->getDomain($slug);
        // otherwise we won't try to guess as it results in silent problems when guess is wrong
        
        // ideally theme has declared its DomainPath
        $target = $theme->get('DomainPath') or
        // if not, we can see if the Domain listener has picked it up 
        $target = Loco_package_Listener::singleton()->getDomainPath($domain);
        // otherwise project will use theme root by default

        
        $bundle->configure( $base, array (
            'Name' => $name,
            'TextDomain' => $domain,
            'DomainPath' => $target,
        ) );
        
        // parent theme inheritance:
        if( $parent = $theme->parent() ){
            try {
                $bundle->parent = self::createFromTheme($parent);
                $bundle->inherit( $bundle->parent );
            }
            catch( Loco_error_Exception $e ){
                Loco_error_AdminNotices::add($e);
            }
        }
        
        // TODO provide hook to modify bundle?
        // do_action( 'loco_bundle_configured', $bundle );

        return $bundle;
    }    
}