calderawpmetaplatecoredata

Class data.

Defined (1)

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

/vendor/calderawp/metaplate-core/src/data.php  
  1. class data { 
  2.  
  3. /** 
  4. * @var string The key for the metaplate registry 
  5. */ 
  6. public static $registry_option_name = '_metaplates_registry'; 
  7.  
  8. /** 
  9. * Get the metaplates for the post 
  10. * @return array active metaplates for this post type 
  11. */ 
  12. public static function get_active_metaplates( ) { 
  13.  
  14. global $post; 
  15.  
  16. // GET METAPLATEs 
  17. $metaplates = self::get_registry(); 
  18. $meta_stack = array(); 
  19. foreach( $metaplates as $metaplate_try ) { 
  20. $is_plate = self::get_metaplate( $metaplate_try['id'] ); 
  21. if( !empty( $is_plate['post_type'][$post->post_type] ) ) { 
  22. switch ($is_plate['page_type']) { 
  23. case 'single': 
  24. if( is_single() || is_page() ) { 
  25. $meta_stack[] = $is_plate; 
  26. break; 
  27. case 'archive': 
  28. if( ( is_archive() || is_front_page() ) && ( !is_page( ) || !is_single( ) ) ) { 
  29. $meta_stack[] = $is_plate; 
  30. break; 
  31. default: 
  32. $meta_stack[] = $is_plate; 
  33. break; 
  34.  
  35. return $meta_stack; 
  36.  
  37.  
  38. /** 
  39. * Merge in Custom field data, meta and post data 
  40. * @return array array with merged data 
  41. */ 
  42. public static function get_custom_field_data( $post_id ) { 
  43.  
  44. global $post; 
  45.  
  46. $raw_data = get_post_meta( $post_id ); 
  47.  
  48. if ( ! is_array( $raw_data ) ) { 
  49. return; 
  50.  
  51.  
  52. // break to standard arrays 
  53. $template_data = array(); 
  54. foreach( $raw_data as $meta_key=>$meta_data ) { 
  55. if ( 0 === strpos( $meta_key, '_' ) ) { 
  56. continue; 
  57.  
  58. if( count( $meta_data ) === 1 ) { 
  59. if( strlen( trim( $meta_data[0] ) ) > 0 ) { // check value is something else leave it out. 
  60. $template_data[$meta_key] = trim( $meta_data[0] ); 
  61. }else{ 
  62. $template_data[$meta_key] = $meta_data; 
  63. // ACF support 
  64. if( class_exists( 'acf' ) ) { 
  65. $fields = get_fields( $post->ID ); 
  66. if ( is_array( $fields ) && ! empty( $fields ) ) { 
  67. $template_data = array_merge( $template_data, $fields ); 
  68.  
  69. // CFS support 
  70. if( class_exists( 'Custom_Field_Suite' ) ) { 
  71. $fields = CFS()->get(); 
  72. if ( is_array( $fields ) && ! empty( $fields ) ) { 
  73. $template_data = array_merge( $template_data, $fields ); 
  74.  
  75.  
  76. //Pods support 
  77. if ( class_exists( 'Pods' ) && false != ( $pods = pods( $post->post_type, $post->ID, true ) ) ) { 
  78. $fields = $pods->export(); 
  79. if ( is_array( $fields ) && ! empty( $fields ) ) { 
  80. $template_data = array_merge( $template_data, $fields ); 
  81.  
  82.  
  83.  
  84. // include post values if in a post 
  85. if( !empty( $post ) ) { 
  86. foreach( $post as $post_key=>$post_value ) { 
  87. $template_data[$post_key] = $post_value; 
  88.  
  89. return $template_data; 
  90.  
  91.  
  92. /** 
  93. * Get a metaplate by ID or slug 
  94. * @param string $id 
  95. * @return array|bool 
  96. */ 
  97. public static function get_metaplate( $id ) { 
  98. $metaplates = self::get_registry(); 
  99. if ( array_key_exists( $id, $metaplates ) ) { 
  100. $metaplate = get_option( $id ); 
  101. else { 
  102. $metaplate = self::get_metaplate_id_by_slug( $id ); 
  103. if ( is_string( $metaplate ) ) { 
  104. $metaplate = self::get_metaplate( $metaplate ); 
  105.  
  106.  
  107.  
  108. if ( is_string( $metaplate ) ) { 
  109. $metaplate = json_decode( $metaplate, ARRAY_A ); 
  110.  
  111. if ( is_object( $metaplate ) ) { 
  112. $metaplate = (array) $metaplate; 
  113.  
  114. $metaplate = self::code( $metaplate ); 
  115.  
  116. return (array) $metaplate; 
  117.  
  118.  
  119. /** 
  120. * Make sure the code fields are arrays not objects and are set 
  121. * @param array $metaplate The metaplate 
  122. * @return array The metaplate with the code fields as arrays 
  123. */ 
  124. private static function code( $metaplate ) { 
  125. foreach ( array( 'html', 'js', 'css' ) as $field ) { 
  126. if ( isset( $metaplate[ $field ] ) ) { 
  127. if ( is_object( $metaplate[ $field ] ) ) { 
  128. $value = $metaplate[ $field ]; 
  129. $value = $value->code; 
  130. unset( $metaplate[ $field ] ); 
  131. $metaplate[ $field ]['code' ] = $value; 
  132.  
  133. else { 
  134. $metaplate[ $field ][ 'code' ] = 1; 
  135.  
  136. return $metaplate; 
  137.  
  138.  
  139. /** 
  140. * Get the metaplate registry 
  141. * @return array|bool 
  142. */ 
  143. public static function get_registry() { 
  144. return get_option( self::$registry_option_name ); 
  145.  
  146. /** 
  147. * Get a metaplate's ID using its slug 
  148. * @param string $slug The metaplate's slug. 
  149. * @param null|array $metaplates Optional. The metaplate registry to look in. 
  150. * @return bool|array 
  151. */ 
  152. public static function get_metaplate_id_by_slug( $slug, $metaplates = null ) { 
  153. if ( is_null( $metaplates ) ) { 
  154. $metaplates = self::get_registry(); 
  155.  
  156. if ( is_array( $metaplates ) ) { 
  157. $search = wp_list_pluck( $metaplates, 'slug' ); 
  158. return array_search( $slug, $search ); 
  159.  
  160.  
  161. return false; 
  162.  
  163. /** 
  164. * Update registry of metaplates 
  165. * Note: Does not save the metaplate itself. 
  166. * @param array $new_value The new item to add. 
  167. * @param string $id Id of new item to add. 
  168. * @return bool 
  169. */ 
  170. public static function update_registry( $new_value, $id ) { 
  171. $registry = self::get_registry(); 
  172. $registry[ $id ] = $new_value; 
  173.  
  174. return update_option( self::$registry_option_name, $registry ); 
  175.  
  176.