| 1 |
<?php |
| 2 |
|
| 3 |
namespace Yatra\Core\Controllers; |
| 4 |
|
| 5 |
class ThemeController |
| 6 |
{ |
| 7 |
protected $theme; |
| 8 |
|
| 9 |
protected $source; |
| 10 |
|
| 11 |
protected $current_theme; |
| 12 |
|
| 13 |
public function __construct($theme, $source = 'wordpress') |
| 14 |
{ |
| 15 |
include_once ABSPATH . 'wp-admin/includes/file.php'; |
| 16 |
include_once ABSPATH . 'wp-admin/includes/misc.php'; |
| 17 |
include_once ABSPATH . 'wp-admin/includes/class-wp-upgrader.php'; |
| 18 |
|
| 19 |
$this->theme = $theme; |
| 20 |
|
| 21 |
$this->set_current_theme(); |
| 22 |
|
| 23 |
$this->source = 'wordpress'; |
| 24 |
} |
| 25 |
|
| 26 |
private function set_current_theme() |
| 27 |
{ |
| 28 |
$this->current_theme = wp_get_theme($this->theme); |
| 29 |
|
| 30 |
} |
| 31 |
|
| 32 |
private function get_source_zip_url() |
| 33 |
{ |
| 34 |
|
| 35 |
return apply_filters('yatra_theme_controller_theme_source', "https://downloads.wordpress.org/theme/{$this->theme}.latest-stable.zip", $this->theme, $this->source); |
| 36 |
} |
| 37 |
|
| 38 |
public function install() |
| 39 |
{ |
| 40 |
|
| 41 |
if ($this->is_installed()) { |
| 42 |
|
| 43 |
return true; |
| 44 |
} |
| 45 |
|
| 46 |
$theme_zip = $this->get_source_zip_url(); |
| 47 |
|
| 48 |
wp_cache_flush(); |
| 49 |
|
| 50 |
$upgrader = new \Theme_Upgrader(); |
| 51 |
|
| 52 |
$upgrader->install($theme_zip); |
| 53 |
|
| 54 |
$this->set_current_theme(); |
| 55 |
|
| 56 |
if ($this->is_installed()) { |
| 57 |
|
| 58 |
return true; |
| 59 |
} |
| 60 |
return false; |
| 61 |
} |
| 62 |
|
| 63 |
public function activate() |
| 64 |
{ |
| 65 |
switch_theme($this->theme); |
| 66 |
|
| 67 |
if ($this->is_activated()) { |
| 68 |
|
| 69 |
return true; |
| 70 |
} |
| 71 |
return false; |
| 72 |
} |
| 73 |
|
| 74 |
public function install_and_activate() |
| 75 |
{ |
| 76 |
if ($this->is_installed()) { |
| 77 |
|
| 78 |
return $this->activate(); |
| 79 |
|
| 80 |
} else { |
| 81 |
|
| 82 |
ob_start(); |
| 83 |
|
| 84 |
$this->install(); |
| 85 |
|
| 86 |
$install = ob_get_clean(); |
| 87 |
|
| 88 |
return $this->activate(); |
| 89 |
|
| 90 |
} |
| 91 |
} |
| 92 |
|
| 93 |
public function is_installed() |
| 94 |
{ |
| 95 |
if ($this->current_theme->exists()) { |
| 96 |
return true; |
| 97 |
} |
| 98 |
return false; |
| 99 |
} |
| 100 |
|
| 101 |
public function is_activated() |
| 102 |
{ |
| 103 |
$active_theme = wp_get_theme(); |
| 104 |
|
| 105 |
if ($active_theme->get_template() == $this->theme) { |
| 106 |
|
| 107 |
return true; |
| 108 |
} |
| 109 |
return false; |
| 110 |
} |
| 111 |
} |