/wp-content/plugins/all-in-one-event-calendar/app/helper/class-ai1ec-view-helper.php

https://gitlab.com/Blueprint-Marketing/interoccupy.net · PHP · 676 lines · 409 code · 58 blank · 209 comment · 41 complexity · ca3f16f9ba1ee3ba1e7cb063fd19c7d2 MD5 · raw file

  1. <?php
  2. //
  3. // class-ai1ec-view-helper.php
  4. // all-in-one-event-calendar
  5. //
  6. // Created by The Seed Studio on 2011-07-13.
  7. //
  8. /**
  9. * Ai1ec_View_Helper class
  10. *
  11. * @package Helpers
  12. * @author time.ly
  13. **/
  14. class Ai1ec_View_Helper {
  15. /**
  16. * _instance class variable
  17. *
  18. * Class instance
  19. *
  20. * @var null | object
  21. **/
  22. private static $_instance = NULL;
  23. /**
  24. * Constructor
  25. *
  26. * Default constructor
  27. **/
  28. private function __construct() { }
  29. /**
  30. * get_instance function
  31. *
  32. * Return singleton instance
  33. *
  34. * @return object
  35. **/
  36. static function get_instance() {
  37. if( self::$_instance === NULL ) {
  38. self::$_instance = new self();
  39. }
  40. return self::$_instance;
  41. }
  42. /**
  43. * Enqueue a script from the theme resources directory.
  44. *
  45. * @param string $name Unique identifer for the script
  46. * @param string $file Filename of the script
  47. * @param array $deps Dependencies of the script
  48. * @param bool $in_footer Whether to add the script to the footer of the page
  49. *
  50. * @return void
  51. */
  52. function theme_enqueue_script( $name, $file, $deps = array(), $in_footer = FALSE ) {
  53. if( ! $file || empty( $file ) ) {
  54. throw new Ai1ec_File_Not_Provided( "You need to specify a script file." );
  55. }
  56. try {
  57. $file_path = $this->get_path_of_js_file_to_load( $file );
  58. // Append core themes version to version string to make sure recently
  59. // updated files are used.
  60. wp_enqueue_script(
  61. $name,
  62. $file_path,
  63. $deps,
  64. AI1EC_VERSION . '-' .
  65. Ai1ec_Meta::get_option( 'ai1ec_themes_version', 1 ),
  66. $in_footer
  67. );
  68. } catch ( Ai1ec_File_Not_Found $e ) {
  69. throw $e;
  70. }
  71. }
  72. /**
  73. * admin_enqueue_style function
  74. *
  75. * @return void
  76. **/
  77. function admin_enqueue_style( $name, $file, $deps = array() ) {
  78. if( ! $file || empty( $file ) ) {
  79. throw new Ai1ec_File_Not_Provided( "You need to specify a style file." );
  80. }
  81. $_file = AI1EC_ADMIN_THEME_CSS_PATH . '/' . $file;
  82. if( ! file_exists( $_file ) ) {
  83. throw new Ai1ec_File_Not_Found( "The specified file " . $file . " doesn't exist." );
  84. } else {
  85. $file = AI1EC_ADMIN_THEME_CSS_URL . '/' . $file;
  86. wp_enqueue_style( $name, $file, $deps, AI1EC_VERSION );
  87. }
  88. }
  89. /**
  90. * theme_enqueue_style function
  91. *
  92. * @return void
  93. **/
  94. function theme_enqueue_style( $name, $file, $deps = array() ) {
  95. global $ai1ec_themes_controller;
  96. if( ! $file || empty( $file ) ) {
  97. throw new Ai1ec_File_Not_Provided( "You need to specify a style file." );
  98. }
  99. // template path
  100. $active_template_path = $ai1ec_themes_controller->active_template_path();
  101. // template url
  102. $active_template_url = $ai1ec_themes_controller->active_template_url();
  103. // look for the file in the active theme
  104. $themes_root = array(
  105. (object) array(
  106. 'path' => $active_template_path . '/' . AI1EC_CSS_FOLDER,
  107. 'url' => $active_template_url . '/' . AI1EC_CSS_FOLDER
  108. ),
  109. (object) array(
  110. 'path' => $active_template_path,
  111. 'url' => $active_template_url
  112. ),
  113. (object) array(
  114. 'path' => AI1EC_DEFAULT_THEME_PATH . '/' . AI1EC_CSS_FOLDER,
  115. 'url' => AI1EC_DEFAULT_THEME_URL . '/' . AI1EC_CSS_FOLDER
  116. ),
  117. (object) array(
  118. 'path' => AI1EC_DEFAULT_THEME_PATH,
  119. 'url' => AI1EC_DEFAULT_THEME_URL
  120. ),
  121. );
  122. $file_found = false;
  123. // look for the file in each theme
  124. foreach( $themes_root as $theme_root ) {
  125. // $_file is a local var to hold the value of
  126. // the file we are looking for
  127. $_file = $theme_root->path . '/' . $file;
  128. if( file_exists( $_file ) ) {
  129. // file is found
  130. $file_found = true;
  131. // assign the found file
  132. $file = $theme_root->url . '/' . $file;
  133. // exit the loop;
  134. break;
  135. }
  136. }
  137. if( $file_found === false ) {
  138. throw new Ai1ec_File_Not_Found( "The specified file '" . $file . "' doesn't exist." );
  139. }
  140. else {
  141. // Append core themes version to version string to make sure recently
  142. // updated files are used.
  143. wp_enqueue_style(
  144. $name,
  145. $file,
  146. $deps,
  147. AI1EC_VERSION . '-' .
  148. Ai1ec_Meta::get_option( 'ai1ec_themes_version', 1 )
  149. );
  150. }
  151. }
  152. /**
  153. * display_admin function
  154. *
  155. * Display the view specified by file $file and passed arguments $args.
  156. *
  157. * @param string $file
  158. * @param array $args
  159. *
  160. * @return void
  161. **/
  162. function display_admin( $file = false, $args = array() ) {
  163. if( ! $file || empty( $file ) ) {
  164. throw new Ai1ec_File_Not_Provided( "You need to specify a view file." );
  165. }
  166. $file = AI1EC_ADMIN_THEME_PATH . '/' . $file;
  167. if( ! file_exists( $file ) ) {
  168. throw new Ai1ec_File_Not_Found( "The specified view file doesn't exist." );
  169. } else {
  170. extract( $args );
  171. require( $file );
  172. }
  173. }
  174. /**
  175. * display_theme function
  176. *
  177. * Display the view specified by file $file and passed arguments $args.
  178. *
  179. * @param string $file
  180. * @param array $args
  181. *
  182. * @return void
  183. **/
  184. function display_theme( $file = false, $args = array() ) {
  185. global $ai1ec_themes_controller;
  186. if( ! $file || empty( $file ) ) {
  187. throw new Ai1ec_File_Not_Provided( "You need to specify a view file." );
  188. }
  189. // look for the file in the selected theme
  190. $themes_root = array(
  191. $ai1ec_themes_controller->active_template_path(),
  192. AI1EC_DEFAULT_THEME_PATH
  193. );
  194. // remove duplicates
  195. $themes_root = array_unique( $themes_root );
  196. $file_found = false;
  197. // look for the file in each theme
  198. foreach( $themes_root as $theme_root ) {
  199. // $_file is a local var to hold the value of
  200. // the file we are looking for
  201. $_file = $theme_root . '/' . $file;
  202. if( file_exists( $_file ) ) {
  203. // file is found
  204. $file_found = true;
  205. // assign the found file
  206. $file = $_file;
  207. // exit the loop;
  208. break;
  209. }
  210. }
  211. if( $file_found === false ) {
  212. throw new Ai1ec_File_Not_Found( "The specified view file '" . $file . "' doesn't exist." );
  213. } else {
  214. extract( $args );
  215. require( $file );
  216. }
  217. }
  218. /**
  219. * display_admin_css function
  220. *
  221. * Renders the given stylesheet inline. If stylesheet has already been
  222. * displayed once before with the same set of $args, does not display
  223. * it again.
  224. *
  225. * @param string $file
  226. * @param array $args
  227. *
  228. * @return void
  229. **/
  230. function display_admin_css( $file = false, $args = array() ) {
  231. static $displayed = array();
  232. if( ! $file || empty( $file ) ) {
  233. throw new Ai1ec_File_Not_Provided( 'You need to specify a css file.' );
  234. }
  235. $file = AI1EC_ADMIN_THEME_CSS_PATH . '/' . $file;
  236. if( isset( $displayed[$file] ) && $displayed[$file] === $args ) // Skip if already displayed
  237. return;
  238. if( ! file_exists( $file ) ) {
  239. throw new Ai1ec_File_Not_Found( "The specified css file doesn't exist." );
  240. } else {
  241. $displayed[$file] = $args; // Flag that we've displayed this file with these args
  242. extract( $args );
  243. echo '<style type="text/css">';
  244. require( $file );
  245. echo '</style>';
  246. }
  247. }
  248. /**
  249. * display_theme_css function
  250. *
  251. * Renders the given stylesheet inline. If stylesheet has already been
  252. * displayed once before with the same set of $args, does not display
  253. * it again.
  254. *
  255. * @param string $file
  256. * @param array $args
  257. *
  258. * @return void
  259. **/
  260. function display_theme_css( $file = false, $args = array() ) {
  261. global $ai1ec_themes_controller;
  262. static $displayed = array();
  263. if( ! $file || empty( $file ) ) {
  264. throw new Ai1ec_File_Not_Provided( 'You need to specify a CSS file.' );
  265. }
  266. // look for the file in the selected theme
  267. $themes_root = array(
  268. $ai1ec_themes_controller->active_template_path() . '/' . AI1EC_THEME_CSS_FOLDER,
  269. AI1EC_DEFAULT_THEME_PATH . '/' . AI1EC_THEME_CSS_FOLDER
  270. );
  271. // remove duplicates
  272. $themes_root = array_unique( $themes_root );
  273. $file_found = false;
  274. // look for the file in each theme
  275. foreach( $themes_root as $theme_root ) {
  276. // $_file is a local var to hold the value of
  277. // the file we are looking for
  278. $_file = $theme_root . '/' . $file;
  279. if( file_exists( $_file ) ) {
  280. // file is found
  281. $file_found = true;
  282. // assign the found file
  283. $file = $_file;
  284. // exit the loop;
  285. break;
  286. }
  287. }
  288. if( isset( $displayed[$file] ) && $displayed[$file] === $args ) // Skip if already displayed
  289. return;
  290. if( ! file_exists( $file ) ) {
  291. throw new Ai1ec_File_Not_Found( "The specified CSS file doesn't exist." );
  292. } else {
  293. $displayed[$file] = $args; // Flag that we've displayed this file with these args
  294. extract( $args );
  295. echo '<style type="text/css">';
  296. require( $file );
  297. echo '</style>';
  298. }
  299. }
  300. /**
  301. * display_admin_js function
  302. *
  303. * Renders the given script inline. If script has already been displayed
  304. * once before with the same set of $args, does not display it again.
  305. *
  306. * @param string $file
  307. * @param array $args
  308. *
  309. * @return void
  310. **/
  311. function display_admin_js( $file = false, $args = array() ) {
  312. static $displayed = array();
  313. if( ! $file || empty( $file ) ) {
  314. throw new Ai1ec_File_Not_Provided( "You need to specify a js file." );
  315. }
  316. $file = AI1EC_ADMIN_THEME_JS_PATH . '/' . $file;
  317. if( $displayed[$file] === $args) // Skip if already displayed
  318. return;
  319. if( ! file_exists( $file ) ) {
  320. throw new Ai1ec_File_Not_Found( "The specified js file doesn't exist." );
  321. } else {
  322. $displayed[$file] = $args; // Flag that we've displayed this file with these args
  323. extract( $args );
  324. echo '<script type="text/javascript" charset="utf-8">';
  325. echo '/* <![CDATA[ */';
  326. require( $file );
  327. echo '/* ]]> */';
  328. echo '</script>';
  329. }
  330. }
  331. /**
  332. * display_theme_js function
  333. *
  334. * Renders the given script inline. If script has already been displayed
  335. * once before with the same set of $args, does not display it again.
  336. *
  337. * @param string $file
  338. * @param array $args
  339. *
  340. * @return void
  341. **/
  342. function display_theme_js( $file = false, $args = array() ) {
  343. global $ai1ec_themes_controller;
  344. static $displayed = array();
  345. if( ! $file || empty( $file ) ) {
  346. throw new Ai1ec_File_Not_Provided( "You need to specify a JS file." );
  347. }
  348. // look for the file in the selected theme
  349. $themes_root = array(
  350. $ai1ec_themes_controller->active_template_path() . '/' . AI1EC_THEME_JS_FOLDER,
  351. AI1EC_DEFAULT_THEME_PATH . '/' . AI1EC_THEME_JS_FOLDER
  352. );
  353. // remove duplicates
  354. $themes_root = array_unique( $themes_root );
  355. $file_found = false;
  356. // look for the file in each theme
  357. foreach( $themes_root as $theme_root ) {
  358. // $_file is a local var to hold the value of
  359. // the file we are looking for
  360. $_file = $theme_root . '/' . $file;
  361. if( file_exists( $_file ) ) {
  362. // file is found
  363. $file_found = true;
  364. // assign the found file
  365. $file = $_file;
  366. // exit the loop;
  367. break;
  368. }
  369. }
  370. if( $displayed[$file] === $args) // Skip if already displayed
  371. return;
  372. if( ! file_exists( $file ) ) {
  373. throw new Ai1ec_File_Not_Found( "The specified JS file doesn't exist." );
  374. } else {
  375. $displayed[$file] = $args; // Flag that we've displayed this file with these args
  376. extract( $args );
  377. echo '<script type="text/javascript" charset="utf-8">';
  378. echo '/* <![CDATA[ */';
  379. require( $file );
  380. echo '/* ]]> */';
  381. echo '</script>';
  382. }
  383. }
  384. /**
  385. * get_admin_view function
  386. *
  387. * Return the output of a view as a string rather than output to response.
  388. *
  389. * @param string $file
  390. * @param array $args
  391. *
  392. * @return void
  393. **/
  394. function get_admin_view( $file = false, $args = array() ) {
  395. ob_start();
  396. $this->display_admin( $file, $args );
  397. return ob_get_clean();
  398. }
  399. /**
  400. * get_theme_view function
  401. *
  402. * Return the output of a view in the theme as a string rather than output to response.
  403. *
  404. * @param string $file
  405. * @param array $args
  406. *
  407. * @return void
  408. **/
  409. function get_theme_view( $file = false, $args = array() ) {
  410. ob_start();
  411. $this->display_theme( $file, $args );
  412. return ob_get_clean();
  413. }
  414. /**
  415. * get_admin_img_url function
  416. *
  417. * @return string
  418. **/
  419. public function get_admin_img_url( $file ) {
  420. if( ! $file || empty( $file ) ) {
  421. throw new Ai1ec_File_Not_Provided( "You need to specify an image file." );
  422. }
  423. $_file = AI1EC_ADMIN_THEME_IMG_PATH . '/' . $file;
  424. if( ! file_exists( $_file ) ) {
  425. throw new Ai1ec_File_Not_Found( "The specified file " . $_file . " doesn't exist." );
  426. } else {
  427. $file = AI1EC_ADMIN_THEME_IMG_URL . '/' . $file;
  428. return $file;
  429. }
  430. }
  431. /**
  432. * get_theme_img_url function
  433. *
  434. * @return string
  435. **/
  436. public function get_theme_img_url( $file ) {
  437. global $ai1ec_themes_controller;
  438. if( ! $file || empty( $file ) ) {
  439. throw new Ai1ec_File_Not_Provided( "You need to specify an image file." );
  440. }
  441. // template path
  442. $active_template_path = $ai1ec_themes_controller->active_template_path();
  443. // template url
  444. $active_template_url = $ai1ec_themes_controller->active_template_url();
  445. // look for the file in the active theme
  446. $themes_root = array(
  447. (object) array(
  448. 'path' => $active_template_path . '/' . AI1EC_IMG_FOLDER,
  449. 'url' => $active_template_url . '/' . AI1EC_IMG_FOLDER
  450. ),
  451. (object) array(
  452. 'path' => AI1EC_DEFAULT_THEME_PATH . '/' . AI1EC_IMG_FOLDER,
  453. 'url' => AI1EC_DEFAULT_THEME_URL . '/' . AI1EC_IMG_FOLDER
  454. ),
  455. );
  456. $file_found = false;
  457. // look for the file in each theme
  458. foreach( $themes_root as $theme_root ) {
  459. // $_file is a local var to hold the value of
  460. // the file we are looking for
  461. $_file = $theme_root->path . '/' . $file;
  462. if( file_exists( $_file ) ) {
  463. // file is found
  464. $file_found = true;
  465. // assign the found file
  466. $file = $theme_root->url . '/' . $file;
  467. // exit the loop;
  468. break;
  469. }
  470. }
  471. if( $file_found === false ) {
  472. throw new Ai1ec_File_Not_Found( "The specified file '" . $file . "' doesn't exist." );
  473. } else {
  474. return $file;
  475. }
  476. }
  477. /**
  478. * Utility for properly outputting JSON data as an AJAX response.
  479. *
  480. * @param array $data
  481. *
  482. * @return void
  483. */
  484. function json_response( $data ) {
  485. header( 'Cache-Control: no-cache, must-revalidate' );
  486. header( 'Pragma: no-cache' );
  487. header( 'Content-Type: application/json; charset=UTF-8' );
  488. // Output JSON-encoded result and quit
  489. echo json_encode( ai1ec_utf8( $data ) );
  490. exit;
  491. }
  492. /**
  493. * Utility for properly outputting JSONP data as an AJAX response.
  494. *
  495. * @param string $data
  496. *
  497. * @param string $callback
  498. *
  499. * @return void
  500. */
  501. function jsonp_response( $data, $callback ) {
  502. header( 'Content-Type: application/json; charset=UTF-8' );
  503. // Output JSONP-encoded result and quit
  504. echo $callback . '(' . json_encode( ai1ec_utf8( $data ) ) . ')';
  505. exit;
  506. }
  507. /**
  508. * Utility for properly outputting XML data as an AJAX response.
  509. *
  510. * @param array $data
  511. *
  512. * @return void
  513. */
  514. function xml_response( $data ) {
  515. header( 'Content-Type: text/xml; charset=UTF-8' );
  516. // Output JSON-encoded result and quit
  517. echo Ai1ec_XML_Utility::serialize_to_xml( ai1ec_utf8( $data ) );
  518. exit;
  519. }
  520. /**
  521. * url method
  522. *
  523. * Generate page link given stub (base) and arguments to inject.
  524. * Some arguments are treated specially. I.e. 'action', which is
  525. * injected using WP add_query_arg method.
  526. *
  527. * @uses add_query_arg To inject special query vars
  528. * @uses Ai1ec_Router::uri() To generate final URI
  529. *
  530. * @param string $page Page stub (base) to extend
  531. * @param array $argv Arguments to add to query
  532. *
  533. * @return string Fully usable URI
  534. */
  535. public function url( $page, array $argv = array() ) {
  536. global $ai1ec_settings, $ai1ec_router;
  537. $action = $ai1ec_settings->default_calendar_view;
  538. extract( $argv, EXTR_IF_EXISTS );
  539. if ( 0 !== strncmp( $action, 'ai1ec_', 6 ) ) {
  540. $action = 'ai1ec_' . $action;
  541. }
  542. if ( false === strpos( $page, '#' ) ) {
  543. add_query_arg( compact( 'action' ), $page );
  544. }
  545. return $ai1ec_router->uri( $argv, $page );
  546. }
  547. /**
  548. * disable_autosave method
  549. *
  550. * Callback to disable autosave script
  551. *
  552. * @param array $input List of scripts registered
  553. *
  554. * @return array Modified scripts list
  555. */
  556. public function disable_autosave( array $input ) {
  557. wp_deregister_script( 'autosave' );
  558. $autosave_key = array_search( 'autosave', $input );
  559. if ( false === $autosave_key || ! is_scalar( $autosave_key ) ) {
  560. unset( $input[$autosave_key] );
  561. }
  562. return $input;
  563. }
  564. /**
  565. * the_title_admin method
  566. *
  567. * Override title, visible in admin side, to display parent event
  568. * title in-line.
  569. *
  570. * @param string $title Title to be displayed
  571. * @param int $post_id ID of post being displayed
  572. *
  573. * @return string Modified title
  574. */
  575. public function the_title_admin( $title, $post_id ) {
  576. global $ai1ec_events_helper;
  577. remove_filter( 'the_title', 'esc_html' );
  578. $title = esc_html( $title );
  579. $parent_post_id = $ai1ec_events_helper->event_parent( $post_id );
  580. if ( $parent_post_id ) {
  581. $parent_post = get_post( $parent_post_id );
  582. if ( NULL !== $parent_post ) {
  583. $title .= '</a> <span class="ai1ec-instance-parent">(' .
  584. __( 'instance of', AI1EC_PLUGIN_NAME ) .
  585. ' <a href="';
  586. $title .= get_edit_post_link(
  587. $parent_post_id,
  588. 'display'
  589. );
  590. $title .= '">' . $parent_post->post_title;
  591. $title .= '</a>)</span><a href="#noclick">';
  592. }
  593. }
  594. return $title;
  595. }
  596. }
  597. // END class