/includes/objects.php

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