Keine Beschreibung

class.jetpack-json-api-themes-modify-endpoint.php 4.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. <?php
  2. class Jetpack_JSON_API_Themes_Modify_Endpoint extends Jetpack_JSON_API_Themes_Endpoint {
  3. // POST /sites/%s/themes/%s
  4. // POST /sites/%s/themes
  5. protected $needed_capabilities = 'update_themes';
  6. protected $action = 'default_action';
  7. protected $expected_actions = array( 'update', 'update_translations' );
  8. public function default_action() {
  9. $args = $this->input();
  10. if ( isset( $args['autoupdate'] ) && is_bool( $args['autoupdate'] ) ) {
  11. if ( $args['autoupdate'] ) {
  12. $this->autoupdate_on();
  13. } else {
  14. $this->autoupdate_off();
  15. }
  16. }
  17. if ( isset( $args['autoupdate_translations'] ) && is_bool( $args['autoupdate_translations'] ) ) {
  18. if ( $args['autoupdate_translations'] ) {
  19. $this->autoupdate_translations_on();
  20. } else {
  21. $this->autoupdate_translations_off();
  22. }
  23. }
  24. return true;
  25. }
  26. function autoupdate_on() {
  27. $autoupdate_themes = Jetpack_Options::get_option( 'autoupdate_themes', array() );
  28. $autoupdate_themes = array_unique( array_merge( $autoupdate_themes, $this->themes ) );
  29. Jetpack_Options::update_option( 'autoupdate_themes', $autoupdate_themes );
  30. }
  31. function autoupdate_off() {
  32. $autoupdate_themes = Jetpack_Options::get_option( 'autoupdate_themes', array() );
  33. $autoupdate_themes = array_diff( $autoupdate_themes, $this->themes );
  34. Jetpack_Options::update_option( 'autoupdate_themes', $autoupdate_themes );
  35. }
  36. function autoupdate_translations_on() {
  37. $autoupdate_themes_translations = Jetpack_Options::get_option( 'autoupdate_themes_translations', array() );
  38. $autoupdate_themes_translations = array_unique( array_merge( $autoupdate_themes_translations, $this->themes ) );
  39. Jetpack_Options::update_option( 'autoupdate_themes_translations', $autoupdate_themes_translations );
  40. }
  41. function autoupdate_translations_off() {
  42. $autoupdate_themes_translations = Jetpack_Options::get_option( 'autoupdate_themes_translations', array() );
  43. $autoupdate_themes_translations = array_diff( $autoupdate_themes_translations, $this->themes );
  44. Jetpack_Options::update_option( 'autoupdate_themes_translations', $autoupdate_themes_translations );
  45. }
  46. function update() {
  47. include_once ABSPATH . 'wp-admin/includes/class-wp-upgrader.php';
  48. // Clear the cache.
  49. wp_update_themes();
  50. foreach ( $this->themes as $theme ) {
  51. /**
  52. * Pre-upgrade action
  53. *
  54. * @since 3.9.3
  55. *
  56. * @param object $theme WP_Theme object
  57. * @param array $themes Array of theme objects
  58. */
  59. do_action('jetpack_pre_theme_upgrade', $theme, $this->themes);
  60. // Objects created inside the for loop to clean the messages for each theme
  61. $skin = new Automatic_Upgrader_Skin();
  62. $upgrader = new Theme_Upgrader( $skin );
  63. $upgrader->init();
  64. $result = $upgrader->upgrade( $theme );
  65. $this->log[ $theme ][] = $upgrader->skin->get_upgrade_messages();
  66. }
  67. if ( ! $this->bulk && ! $result ) {
  68. return new WP_Error( 'update_fail', __( 'There was an error updating your theme', 'jetpack' ), 400 );
  69. }
  70. return true;
  71. }
  72. function update_translations() {
  73. include_once ABSPATH . 'wp-admin/includes/class-wp-upgrader.php';
  74. // Clear the cache.
  75. wp_update_themes();
  76. $available_themes_updates = get_site_transient( 'update_themes' );
  77. if ( ! isset( $available_themes_updates->translations ) || empty( $available_themes_updates->translations ) ) {
  78. return new WP_Error( 'nothing_to_translate' );
  79. }
  80. foreach( $available_themes_updates->translations as $translation ) {
  81. $theme = $translation['slug'] ;
  82. if ( ! in_array( $translation['slug'], $this->themes ) ) {
  83. $this->log[ $theme ][] = __( 'No update needed', 'jetpack' );
  84. continue;
  85. }
  86. /**
  87. * Pre-upgrade action
  88. *
  89. * @since 4.4.0
  90. *
  91. * @param object $theme WP_Theme object
  92. * @param array $themes Array of theme objects
  93. */
  94. do_action( 'jetpack_pre_theme_upgrade_translations', $theme, $this->themes );
  95. // Objects created inside the for loop to clean the messages for each theme
  96. $skin = new Automatic_Upgrader_Skin();
  97. $upgrader = new Language_Pack_Upgrader( $skin );
  98. $upgrader->init();
  99. $result = $upgrader->upgrade( (object) $translation );
  100. $this->log[ $theme ] = $upgrader->skin->get_upgrade_messages();
  101. }
  102. if ( ! $this->bulk && ! $result ) {
  103. return new WP_Error( 'update_fail', __( 'There was an error updating your theme', 'jetpack' ), 400 );
  104. }
  105. return true;
  106. }
  107. }