Jetpack_Sync_Module_Themes

The WordPress Core Jetpack Sync Module Themes class.

Defined (1)

The class is defined in the following location(s).

/sync/class.jetpack-sync-module-themes.php  
  1. class Jetpack_Sync_Module_Themes extends Jetpack_Sync_Module { 
  2. function name() { 
  3. return 'themes'; 
  4.  
  5. public function init_listeners( $callable ) { 
  6. add_action( 'switch_theme', array( $this, 'sync_theme_support' ) ); 
  7. add_action( 'jetpack_sync_current_theme_support', $callable ); 
  8. add_action( 'upgrader_process_complete', array( $this, 'check_upgrader'), 10, 2 ); 
  9. add_action( 'jetpack_installed_theme', $callable, 10, 2 ); 
  10. add_action( 'jetpack_updated_theme', $callable, 10, 2 ); 
  11.  
  12. // Sidebar updates. 
  13. add_action( 'update_option_sidebars_widgets', array( $this, 'sync_sidebar_widgets_actions' ), 10, 2 ); 
  14. add_action( 'jetpack_widget_added', $callable, 10, 2 ); 
  15. add_action( 'jetpack_widget_removed', $callable, 10, 2 ); 
  16. add_action( 'jetpack_widget_moved_to_inactive', $callable ); 
  17. add_action( 'jetpack_cleared_inactive_widgets', $callable ); 
  18. add_action( 'jetpack_widget_reordered', $callable ); 
  19.  
  20. public function check_upgrader( $upgrader, $details) { 
  21. if ( ! isset( $details['type'] ) || 
  22. 'theme' !== $details['type'] || 
  23. is_wp_error( $upgrader->skin->result ) || 
  24. ! method_exists( $upgrader, 'theme_info' ) 
  25. ) { 
  26. return; 
  27.  
  28. $theme = $upgrader->theme_info(); 
  29. $theme_info = array( 
  30. 'name' => $theme->get( 'Name' ),  
  31. 'version' => $theme->get( 'Version' ),  
  32. 'uri' => $theme->get( 'ThemeURI' ),  
  33. ); 
  34.  
  35. if ( 'install' === $details['action'] ) { 
  36. /** 
  37. * Signals to the sync listener that a theme was installed and a sync action 
  38. * reflecting the installation and the theme info should be sent 
  39. * @since 4.9.0 
  40. * @param string $theme->theme_root Text domain of the theme 
  41. * @param mixed $theme_info Array of abbreviated theme info 
  42. */ 
  43. do_action( 'jetpack_installed_theme', $theme->stylesheet, $theme_info ); 
  44.  
  45. if ( 'update' === $details['action'] ) { 
  46. /** 
  47. * Signals to the sync listener that a theme was updated and a sync action 
  48. * reflecting the update and the theme info should be sent 
  49. * @since 4.9.0 
  50. * @param string $theme->theme_root Text domain of the theme 
  51. * @param mixed $theme_info Array of abbreviated theme info 
  52. */ 
  53. do_action( 'jetpack_updated_theme', $theme->stylesheet, $theme_info ); 
  54.  
  55. public function init_full_sync_listeners( $callable ) { 
  56. add_action( 'jetpack_full_sync_theme_data', $callable ); 
  57.  
  58. public function sync_theme_support() { 
  59. /** 
  60. * Fires when the client needs to sync theme support info 
  61. * Only sends theme support attributes whitelisted in Jetpack_Sync_Defaults::$default_theme_support_whitelist 
  62. * @since 4.2.0 
  63. * @param object the theme support hash 
  64. */ 
  65. do_action( 'jetpack_sync_current_theme_support' , $this->get_theme_support_info() ); 
  66.  
  67. public function enqueue_full_sync_actions( $config, $max_items_to_enqueue, $state ) { 
  68. /** 
  69. * Tells the client to sync all theme data to the server 
  70. * @since 4.2.0 
  71. * @param boolean Whether to expand theme data (should always be true) 
  72. */ 
  73. do_action( 'jetpack_full_sync_theme_data', true ); 
  74.  
  75. // The number of actions enqueued, and next module state (true == done) 
  76. return array( 1, true ); 
  77.  
  78. public function estimate_full_sync_actions( $config ) { 
  79. return 1; 
  80.  
  81. public function init_before_send() { 
  82. add_filter( 'jetpack_sync_before_send_jetpack_full_sync_theme_data', array( $this, 'expand_theme_data' ) ); 
  83.  
  84. function get_full_sync_actions() { 
  85. return array( 'jetpack_full_sync_theme_data' ); 
  86.  
  87. function expand_theme_data() { 
  88. return array( $this->get_theme_support_info() ); 
  89.  
  90. function sync_add_widgets_to_sidebar( $new_widgets, $old_widgets, $sidebar ) { 
  91. $added_widgets = array_diff( $new_widgets, $old_widgets ); 
  92. if ( empty( $added_widgets ) ) { 
  93. return array(); 
  94. $moved_to_sidebar = array(); 
  95. foreach ( $added_widgets as $added_widget ) { 
  96. $moved_to_sidebar[] = $added_widget; 
  97. /** 
  98. * Helps Sync log that a widget got added 
  99. * @since 4.9.0 
  100. * @param string $sidebar, Sidebar id got changed 
  101. * @param string $added_widget, Widget id got added 
  102. */ 
  103. do_action( 'jetpack_widget_added', $sidebar, $added_widget ); 
  104. return $moved_to_sidebar; 
  105.  
  106. function sync_remove_widgets_from_sidebar( $new_widgets, $old_widgets, $sidebar, $inactive_widgets ) { 
  107. $removed_widgets = array_diff( $old_widgets, $new_widgets ); 
  108.  
  109. if ( empty( $removed_widgets ) ) { 
  110. return array(); 
  111.  
  112. $moved_to_inactive = array(); 
  113.  
  114. foreach( $removed_widgets as $removed_widget ) { 
  115. // Lets check if we didn't move the widget to in_active_widgets 
  116. if ( isset( $inactive_widgets ) && ! in_array( $removed_widget, $inactive_widgets ) ) { 
  117. /** 
  118. * Helps Sync log that a widgte got removed 
  119. * @since 4.9.0 
  120. * @param string $sidebar, Sidebar id got changed 
  121. * @param string $removed_widget, Widget id got removed 
  122. */ 
  123. do_action( 'jetpack_widget_removed', $sidebar, $removed_widget ); 
  124. } else { 
  125. $moved_to_inactive[] = $removed_widget; 
  126. return $moved_to_inactive; 
  127.  
  128.  
  129. function sync_widgets_reordered( $new_widgets, $old_widgets, $sidebar ) { 
  130. $added_widgets = array_diff( $new_widgets, $old_widgets ); 
  131. if ( ! empty( $added_widgets ) ) { 
  132. return; 
  133. $removed_widgets = array_diff( $old_widgets, $new_widgets ); 
  134. if ( ! empty( $removed_widgets ) ) { 
  135. return; 
  136.  
  137. if ( serialize( $old_widgets ) !== serialize( $new_widgets ) ) { 
  138. /** 
  139. * Helps Sync log that a sidebar id got reordered 
  140. * @since 4.9.0 
  141. * @param string $sidebar, Sidebar id got changed 
  142. */ 
  143. do_action( 'jetpack_widget_reordered', $sidebar ); 
  144.  
  145.  
  146. function sync_sidebar_widgets_actions( $old_value, $new_value ) { 
  147.  
  148. // Don't really know how to deal with different array_values yet. 
  149. if ( $old_value['array_version'] !== 3 || $new_value['array_version'] !== 3 ) { 
  150. return; 
  151.  
  152. $moved_to_inactive = array(); 
  153. $moved_to_sidebar = array(); 
  154.  
  155. foreach ( $new_value as $sidebar => $new_widgets ) { 
  156. if ( in_array( $sidebar, array( 'array_version', 'wp_inactive_widgets' ) ) ) { 
  157. continue; 
  158. $old_widgets = isset( $old_value[ $sidebar ] ) 
  159. ? $old_value[ $sidebar ] 
  160. : array(); 
  161.  
  162. $moved_to_inactive_recently = $this->sync_remove_widgets_from_sidebar( $new_widgets, $old_widgets, $sidebar, $new_value['wp_inactive_widgets'] ); 
  163. $moved_to_inactive = array_merge( $moved_to_inactive, $moved_to_inactive_recently ); 
  164.  
  165.  
  166. $moved_to_sidebar_recently = $this->sync_add_widgets_to_sidebar( $new_widgets, $old_widgets, $sidebar ); 
  167. $moved_to_sidebar = array_merge( $moved_to_sidebar, $moved_to_sidebar_recently ); 
  168.  
  169. $this->sync_widgets_reordered( $new_widgets, $old_widgets, $sidebar ); 
  170.  
  171.  
  172. // Treat inactive sidebar a bit differently 
  173. if ( ! empty( $moved_to_inactive ) ) { 
  174. /** 
  175. * Helps Sync log that a widgets IDs got moved to in active 
  176. * @since 4.9.0 
  177. * @param array $sidebar, Sidebar id got changed 
  178. */ 
  179. do_action( 'jetpack_widget_moved_to_inactive', $moved_to_inactive ); 
  180. } elseif ( empty( $moved_to_sidebar ) && 
  181. empty( $new_value['wp_inactive_widgets']) && 
  182. ! empty( $old_value['wp_inactive_widgets'] ) ) { 
  183. /** 
  184. * Helps Sync log that a got cleared from inactive. 
  185. * @since 4.9.0 
  186. */ 
  187. do_action( 'jetpack_cleared_inactive_widgets' ); 
  188. }  
  189.  
  190. private function get_theme_support_info() { 
  191. global $_wp_theme_features; 
  192.  
  193. $theme_support = array(); 
  194.  
  195. foreach ( Jetpack_Sync_Defaults::$default_theme_support_whitelist as $theme_feature ) { 
  196. $has_support = current_theme_supports( $theme_feature ); 
  197. if ( $has_support ) { 
  198. $theme_support[ $theme_feature ] = $_wp_theme_features[ $theme_feature ]; 
  199.  
  200. $theme = wp_get_theme(); 
  201. $theme_support['name'] = $theme->name; 
  202. $theme_support['version'] = $theme->version; 
  203.  
  204. return $theme_support;