| 1 |
<?php |
| 2 |
/* |
| 3 |
* This file is part of the ManageWP Worker plugin. |
| 4 |
* |
| 5 |
* (c) ManageWP LLC <[email protected]> |
| 6 |
* |
| 7 |
* For the full copyright and license information, please view the LICENSE |
| 8 |
* file that was distributed with this source code. |
| 9 |
*/ |
| 10 |
|
| 11 |
class MWP_WordPress_Provider_Theme implements MWP_WordPress_Provider_Interface |
| 12 |
{ |
| 13 |
const STATUS_ACTIVE = 'active'; |
| 14 |
|
| 15 |
const STATUS_INACTIVE = 'inactive'; |
| 16 |
|
| 17 |
const STATUS_INHERITED = 'inherited'; |
| 18 |
|
| 19 |
private $context; |
| 20 |
|
| 21 |
public function __construct(MWP_WordPress_Context $context) |
| 22 |
{ |
| 23 |
$this->context = $context; |
| 24 |
} |
| 25 |
|
| 26 |
public function fetch(array $options = array()) |
| 27 |
{ |
| 28 |
$rawThemes = $this->context->getThemes(); |
| 29 |
$currentTheme = $this->context->getCurrentTheme(); |
| 30 |
$activeThemeSlug = $currentTheme['Stylesheet']; |
| 31 |
$themes = array(); |
| 32 |
|
| 33 |
$themeInfo = array( |
| 34 |
'name' => 'Name', |
| 35 |
// Absolute path to theme directory. |
| 36 |
'root' => 'Theme Root', |
| 37 |
// Absolute URL to theme directory. |
| 38 |
'rootUri' => 'Theme Root URI', |
| 39 |
|
| 40 |
'version' => 'Version', |
| 41 |
'description' => 'Description', |
| 42 |
'author' => 'Author', |
| 43 |
'authorUri' => 'Author URI', |
| 44 |
'status' => 'Status', |
| 45 |
'parent' => 'Parent Theme', |
| 46 |
); |
| 47 |
|
| 48 |
if (empty($options['fetchDescription'])) { |
| 49 |
unset($themeInfo['description']); |
| 50 |
} |
| 51 |
|
| 52 |
foreach ($rawThemes as $rawTheme) { |
| 53 |
$theme = array( |
| 54 |
// Theme directory, followed by slash and slug, to keep it consistent with plugin info; ie. "twentytwelve/twentytwelve". |
| 55 |
'basename' => $rawTheme['Template'].'/'.$rawTheme['Stylesheet'], |
| 56 |
// A.k.a. "stylesheet", for some reason. This is the theme identifier; ie. "twentytwelve". |
| 57 |
'slug' => $rawTheme['Stylesheet'], |
| 58 |
'children' => array(), |
| 59 |
); |
| 60 |
|
| 61 |
foreach ($themeInfo as $property => $info) { |
| 62 |
if (empty($rawTheme[$info])) { |
| 63 |
$theme[$property] = null; |
| 64 |
continue; |
| 65 |
} |
| 66 |
|
| 67 |
$theme[$property] = $this->context->seemsUtf8($rawTheme[$info]) ? $rawTheme[$info] : utf8_encode($rawTheme[$info]); |
| 68 |
} |
| 69 |
|
| 70 |
// Check if this is the active theme |
| 71 |
$theme['active'] = ($theme['slug'] === $activeThemeSlug); |
| 72 |
|
| 73 |
$themes[$theme['name']] = $theme; |
| 74 |
} |
| 75 |
|
| 76 |
foreach ($themes as &$theme) { |
| 77 |
if (empty($theme['parent']) || empty($themes[$theme['parent']])) { |
| 78 |
continue; |
| 79 |
} |
| 80 |
|
| 81 |
$themes[$theme['parent']]['children'][] = $theme['slug']; |
| 82 |
$theme['parent'] = $themes[$theme['parent']]['slug']; |
| 83 |
} |
| 84 |
|
| 85 |
return array_values($themes); |
| 86 |
} |
| 87 |
} |
| 88 |
|