SimpleCalendarObjects

Objects factory.

Defined (1)

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

/includes/objects.php  
  1. class Objects { 
  2.  
  3. /** 
  4. * Constructor. 
  5. * Add default objects. 
  6. * @since 3.0.0 
  7. */ 
  8. public function __construct() { 
  9.  
  10. // Add default feed type. 
  11. add_filter( 'simcal_get_feed_types', function( $feed_types ) { 
  12. return array_merge( $feed_types, array( 
  13. 'google',  
  14. 'grouped-calendars',  
  15. ) ); 
  16. }, 10, 1 ); 
  17.  
  18. // Add default calendar type. 
  19. add_filter( 'simcal_get_calendar_types', function( $calendar_types ) { 
  20. return array_merge( $calendar_types, array( 
  21. 'default-calendar' => array( 
  22. 'grid',  
  23. 'list',  
  24. ),  
  25. ) ); 
  26. }, 10, 1 ); 
  27.  
  28. // Add default admin objects. 
  29. if ( $is_admin = is_admin() ) { 
  30. add_filter( 'simcal_get_admin_pages', function( $admin_pages ) { 
  31. return array_merge( $admin_pages, array( 
  32. 'add-ons' => array( 
  33. 'add-ons',  
  34. ),  
  35. 'settings' => array( 
  36. 'feeds',  
  37. 'calendars',  
  38. 'advanced',  
  39. ),  
  40. 'tools' => array( 
  41. 'system-status',  
  42. ),  
  43. ) ); 
  44. }, 10, 1 ); 
  45.  
  46. do_action( 'simcal_load_objects', $is_admin ); 
  47.  
  48. /** 
  49. * Get feed types. 
  50. * @since 3.0.0 
  51. * @return array 
  52. */ 
  53. public function get_feed_types() { 
  54. $array = apply_filters( 'simcal_get_feed_types', array() ); 
  55. ksort( $array ); 
  56. return $array; 
  57.  
  58. /** 
  59. * Get calendar types. 
  60. * @since 3.0.0 
  61. * @return array 
  62. */ 
  63. public function get_calendar_types() { 
  64. $array = apply_filters( 'simcal_get_calendar_types', array() ); 
  65. ksort( $array ); 
  66. return $array; 
  67.  
  68. /** 
  69. * Get admin pages. 
  70. * @since 3.0.0 
  71. * @return array 
  72. */ 
  73. public function get_admin_pages() { 
  74. return apply_filters( 'simcal_get_admin_pages', array() ); 
  75.  
  76. /** 
  77. * Get a calendar. 
  78. * Returns the right type of calendar. 
  79. * @since 3.0.0 
  80. * @param int|string|object|\WP_Post|Object\Calendar $object 
  81. * @return null|Object\Calendar 
  82. */ 
  83. public function get_calendar( $object ) { 
  84.  
  85. if ( is_string( $object ) ) { 
  86. return ! empty( $object ) ? $this->get_object( $object, 'calendar', '' ) : null; 
  87.  
  88. if ( is_object( $object ) ) { 
  89. if ( $object instanceof Object\Calendar ) { 
  90. return $this->get_object( $object->type, 'feed', $object ); 
  91. } elseif ( $object instanceof \WP_Post ) { 
  92. if ( $type = wp_get_object_terms( $object->ID, 'calendar_type' ) ) { 
  93. $name = sanitize_title( current( $type )->name ); 
  94. return $this->get_object( $name, 'calendar', $object ); 
  95. } elseif ( isset( $object->type ) && isset( $object->id ) ) { 
  96. return $this->get_object( $object->type, 'calendar', $object->id ); 
  97.  
  98. if ( is_int( $object ) ) { 
  99. $post = get_post( $object ); 
  100. if ( $post && ( $type = wp_get_object_terms( $post->ID, 'calendar_type' ) ) ) { 
  101. $name = sanitize_title( current( $type )->name ); 
  102. return $this->get_object( $name, 'calendar', $post ); 
  103.  
  104. return null; 
  105.  
  106. /** 
  107. * Get a calendar view. 
  108. * @since 3.0.0 
  109. * @param int $id Feed post id. 
  110. * @param string $name (optional) Name of calendar view. 
  111. * @return null|Object\Calendar_View 
  112. */ 
  113. public function get_calendar_view( $id = 0, $name = '' ) { 
  114.  
  115. if ( ! $name && $id > 0 ) { 
  116.  
  117. $calendar_view = get_post_meta( $id, '_calendar_view', true ); 
  118.  
  119. if ( $terms = wp_get_object_terms( $id, 'calendar_type' ) ) { 
  120. $calendar_type = sanitize_title( current( $terms )->name ); 
  121. $name = isset( $calendar_view[ $calendar_type ] ) ? $calendar_type . '-' . $calendar_view[ $calendar_type ] : ''; 
  122.  
  123.  
  124. return $name ? $this->get_object( $name, 'calendar-view', '' ) : null; 
  125.  
  126. /** 
  127. * Get a feed. 
  128. * Returns the right type of feed. 
  129. * @since 3.0.0 
  130. * @param int|string|object|\WP_Post|Object\Calendar $object 
  131. * @return null|Object\Feed 
  132. */ 
  133. public function get_feed( $object ) { 
  134.  
  135. if ( is_string( $object ) ) { 
  136. return ! empty( $object ) ? $this->get_object( $object, 'feed', '' ) : null; 
  137.  
  138. if ( is_object( $object ) ) { 
  139. if ( $object instanceof Object\Calendar ) { 
  140. $feed_name = ''; 
  141. if ( empty( $object->feed ) ) { 
  142. if ( $feed_type = wp_get_object_terms( $object->id, 'feed_type' ) ) { 
  143. $feed_name = sanitize_title( current( $feed_type )->name ); 
  144. } else { 
  145. $feed_name = $object->feed; 
  146. return $this->get_object( $feed_name, 'feed', $object ); 
  147. } elseif ( $object instanceof \WP_Post ) { 
  148. $calendar = $this->get_calendar( $object ); 
  149.  
  150. if ( isset( $calendar->feed ) ) { 
  151. return $this->get_object( $calendar->feed, 'feed', $calendar ); 
  152. } else { 
  153. return null; 
  154.  
  155.  
  156. } elseif ( isset( $object->feed ) && isset( $object->id ) ) { 
  157. return $this->get_object( $object->feed, 'feed', $object ); 
  158.  
  159. if ( is_int( $object ) ) { 
  160. $calendar = $this->get_calendar( $object ); 
  161. return isset( $calendar->feed ) ? $this->get_object( $calendar->feed, 'feed', $calendar ) : null; 
  162.  
  163. return null; 
  164.  
  165. /** 
  166. * Get a field. 
  167. * @since 3.0.0 
  168. * @param array $args Field args. 
  169. * @param string $name Field type. 
  170. * @return null|Object\Field 
  171. */ 
  172. public function get_field( $args, $name = '' ) { 
  173.  
  174. if ( empty( $name ) ) { 
  175. $name = isset( $args['type'] ) ? $args['type'] : false; 
  176.  
  177. return $name ? $this->get_object( $name, 'field', $args ) : null; 
  178.  
  179. /** 
  180. * Get a settings page. 
  181. * @since 3.0.0 
  182. * @param string $name 
  183. * @return null|Object\Admin_Page 
  184. */ 
  185. public function get_admin_page( $name ) { 
  186. return $name ? $this->get_object( $name, 'admin-page' ) : null; 
  187.  
  188. /** 
  189. * Get a plugin object. 
  190. * @since 3.0.0 
  191. * @access private 
  192. * @param string $name Object name. 
  193. * @param string $type Object type. 
  194. * @param mixed $args (optional) arguments for the class constructor. 
  195. * @return null|Object 
  196. */ 
  197. private function get_object( $name, $type, $args = '' ) { 
  198.  
  199. $types = array( 
  200. 'admin-page',  
  201. 'calendar',  
  202. 'calendar-view',  
  203. 'feed',  
  204. 'field',  
  205. ); 
  206.  
  207. if ( in_array( $type, $types ) ) { 
  208.  
  209. $class_name = $this->make_class_name( $name, $type ); 
  210. $parent = '\\' . __NAMESPACE__ . '\Abstracts\\' . implode( '_', array_map( 'ucfirst', explode( '-', $type ) ) ); 
  211. $class = class_exists( $class_name ) ? new $class_name( $args ) : false; 
  212.  
  213. return $class instanceof $parent ? $class : null; 
  214.  
  215. return null; 
  216.  
  217. /** 
  218. * Make class name from slug. 
  219. * Standardizes object naming and class names: <object-name> becomes <Class_Name>. 
  220. * The plugin autoloader uses a similar pattern. 
  221. * @since 3.0.0 
  222. * @access private 
  223. * @param string $name Object name. 
  224. * @param string $type Object type. 
  225. * @return string The class name complete with its full namespace. 
  226. */ 
  227. private function make_class_name( $name, $type ) { 
  228.  
  229. if ( 'calendar' == $type ) { 
  230. $namespace = '\\' . __NAMESPACE__ . '\Calendars\\'; 
  231. } elseif ( 'calendar-view' == $type ) { 
  232. $namespace = '\\' . __NAMESPACE__ . '\Calendars\Views\\'; 
  233. } elseif ( 'feed' == $type ) { 
  234. $namespace = '\\' . __NAMESPACE__ . '\Feeds\\'; 
  235. } elseif ( 'field' == $type ) { 
  236. $namespace = '\\' . __NAMESPACE__ . '\Admin\Fields\\'; 
  237. } elseif ( 'admin-page' == $type ) { 
  238. $namespace = '\\' . __NAMESPACE__ . '\Admin\Pages\\'; 
  239. } else { 
  240. return ''; 
  241.  
  242. $class_name = implode( '_', array_map( 'ucfirst', explode( '-', $name ) ) ); 
  243.  
  244. return $namespace . $class_name; 
  245.