advagg.api.php

  1. cis7 sites/all/modules/ulmus/advagg/advagg.api.php
  2. cle7 sites/all/modules/ulmus/advagg/advagg.api.php
  3. ecd7 sites/all/modules/ulmus/advagg/advagg.api.php
  4. elmsmedia7 sites/all/modules/ulmus/advagg/advagg.api.php
  5. harmony7 sites/all/modules/ulmus/advagg/advagg.api.php
  6. icor7 sites/all/modules/ulmus/advagg/advagg.api.php
  7. meedjum_blog7 sites/all/modules/ulmus/advagg/advagg.api.php
  8. mooc7 sites/all/modules/ulmus/advagg/advagg.api.php

Hooks provided by the AdvAgg module.

Functions

Namesort descending Description
hook_advagg_build_aggregate_plans_alter Allow modules to modify the aggregate plan.
hook_advagg_changed_files Let other modules know about the changed files.
hook_advagg_context_alter Allow other modules to modify $css_groups right before it is processed.
hook_advagg_css_groups_alter Allow other modules to modify $css_groups right before it is processed.
hook_advagg_current_hooks_hash_array_alter Allow other modules to add in their own settings and hooks.
hook_advagg_get_css_aggregate_contents_alter Allow other modules to modify this aggregates contents.
hook_advagg_get_css_file_contents_alter Allow other modules to modify this files contents.
hook_advagg_get_js_aggregate_contents_alter Allow other modules to modify this aggregates contents.
hook_advagg_get_js_file_contents_alter Allow other modules to modify this files contents.
hook_advagg_get_root_files_dir Allow other modules to alter css and js paths.
hook_advagg_js_groups_alter Allow other modules to modify $js_groups right before it is processed.
hook_advagg_modify_css_pre_render_alter Allow other modules to modify $children & $elements before they are rendered.
hook_advagg_modify_js_pre_render_alter Allow other modules to modify $children & $elements before they are rendered.
hook_advagg_save_aggregate_alter Allow other modules to alter the contents and add new files to save (.gz).

File

sites/all/modules/ulmus/advagg/advagg.api.php
View source
  1. <?php
  2. /**
  3. * @file
  4. * Hooks provided by the AdvAgg module.
  5. */
  6. /**
  7. * @addtogroup hooks
  8. * @{
  9. */
  10. /**
  11. * Allow modules to modify the aggregate plan.
  12. *
  13. * @param array $files
  14. * An associative array.
  15. * filename - data
  16. * @param bool $modified
  17. * Set this to TRUE if the $files structure has been changed.
  18. * @param string $type
  19. * css or js.
  20. *
  21. * @see advagg_build_aggregate_plans()
  22. * @see advagg_advagg_build_aggregate_plans_alter()
  23. */
  24. function hook_advagg_build_aggregate_plans_alter(&$files, &$modified, $type) {
  25. // Do nothing if core grouping is disabled.
  26. if (!variable_get('advagg_core_groups', ADVAGG_CORE_GROUPS)) {
  27. return;
  28. }
  29. $temp_new_files = array();
  30. $counter = 0;
  31. foreach ($files as $filename => $data) {
  32. $group = NULL;
  33. $every_page = NULL;
  34. foreach ($data['files'] as $fileinfo) {
  35. // Grouped by group & every_page variables.
  36. if (is_null($group)) {
  37. $group = $fileinfo['group'];
  38. }
  39. if (is_null($every_page)) {
  40. $every_page = $fileinfo['every_page'];
  41. }
  42. // Bump Counter if group/every_page has changed from the last one.
  43. if ($group != $fileinfo['group'] || $every_page != $fileinfo['every_page']) {
  44. ++$counter;
  45. $group = $fileinfo['group'];
  46. $every_page = $fileinfo['every_page'];
  47. $modified = TRUE;
  48. }
  49. $temp_new_files[$counter][] = $fileinfo;
  50. }
  51. ++$counter;
  52. }
  53. // Replace $files array with new aggregate filenames.
  54. $files = advagg_generate_filenames(array($temp_new_files), $type);
  55. }
  56. /**
  57. * Let other modules know about the changed files.
  58. *
  59. * @param array $files
  60. * An associative array.
  61. * filename - meta_data
  62. * @param array $types
  63. * Array containing css and/or js.
  64. *
  65. * @return array
  66. * Not used currently.
  67. *
  68. * @see advagg_push_new_changes()
  69. * @see advagg_js_compress_advagg_changed_files()
  70. */
  71. function hook_advagg_changed_files($files, $types) {
  72. // Only care about js files.
  73. if (empty($types['js'])) {
  74. return;
  75. }
  76. $return = array();
  77. foreach ($files as $filename => $meta_data) {
  78. // Only care about js files.
  79. $ext = pathinfo($filename, PATHINFO_EXTENSION);
  80. if ($ext != 'js') {
  81. continue;
  82. }
  83. $return[$filename] = advagg_js_compress_run_test($filename);
  84. }
  85. return $return;
  86. }
  87. /**
  88. * Allow other modules to add in their own settings and hooks.
  89. *
  90. * @param array $aggregate_settings
  91. * An associative array of hooks and settings used.
  92. *
  93. * @see advagg_current_hooks_hash_array()
  94. * @see advagg_js_compress_advagg_current_hooks_hash_array_alter()
  95. */
  96. function hook_advagg_current_hooks_hash_array_alter(&$aggregate_settings) {
  97. $aggregate_settings['variables']['advagg_js_compressor'] = variable_get('advagg_js_compressor', ADVAGG_JS_COMPRESSOR);
  98. $aggregate_settings['variables']['advagg_js_compress_packer'] = variable_get('advagg_js_compress_packer', ADVAGG_JS_COMPRESS_PACKER);
  99. $aggregate_settings['variables']['advagg_js_max_compress_ratio'] = variable_get('advagg_js_max_compress_ratio', ADVAGG_JS_MAX_COMPRESS_RATIO);
  100. }
  101. /**
  102. * Allow other modules to alter the contents and add new files to save (.gz).
  103. *
  104. * @param array $files_to_save
  105. * array($uri => $contents)
  106. * @param array $aggregate_settings
  107. * array of settings.
  108. * @param array $other_parameters
  109. * array of containing $files & $type.
  110. *
  111. * @see advagg_save_aggregate()
  112. * @see advagg_advagg_save_aggregate_alter()
  113. */
  114. function hook_advagg_save_aggregate_alter(&$files_to_save, $aggregate_settings, $other_parameters) {
  115. // Return if gzip is disabled.
  116. if (empty($aggregate_settings['variables']['advagg_gzip'])) {
  117. return;
  118. }
  119. // See if a .gz file already exists.
  120. $gzip_exists = FALSE;
  121. foreach ($files_to_save as $uri => $contents) {
  122. // See if this uri contains .gz near the end of it.
  123. $pos = strripos($uri, '.gz', 91 + strlen(ADVAGG_SPACE) * 3);
  124. if (!empty($pos)) {
  125. $len = strlen($uri);
  126. // .gz file exists, exit loop.
  127. if ($pos == $len - 3) {
  128. $gzip_exists = TRUE;
  129. break;
  130. }
  131. }
  132. }
  133. // If a .gz file does not exist, create one.
  134. if (!$gzip_exists) {
  135. // Use the first file in the array.
  136. $data = reset($files_to_save);
  137. $uri = key($files_to_save);
  138. // Compress it and add it to the $files_to_save array.
  139. $compressed = gzencode($data, 9, FORCE_GZIP);
  140. $files_to_save[$uri . '.gz'] = $compressed;
  141. }
  142. }
  143. /**
  144. * Allow other modules to alter css and js paths.
  145. *
  146. * @param array $css_paths
  147. * Array containing the local path and url path.
  148. * @param array $js_paths
  149. * Array containing the local path and url path.
  150. *
  151. * @see advagg_get_root_files_dir()
  152. * @see advagg_mod_advagg_get_root_files_dir_alter()
  153. */
  154. function hook_advagg_get_root_files_dir(&$css_paths, &$js_paths) {
  155. $dir = rtrim(variable_get('advagg_mod_unified_multisite_dir', ''), '/');
  156. if (empty($dir) || !file_exists($dir) || !is_dir($dir)) {
  157. return;
  158. }
  159. // Change directory.
  160. $css_paths[0] = $dir . '/advagg_css';
  161. $js_paths[0] = $dir . '/advagg_js';
  162. file_prepare_directory($css_paths[0], FILE_CREATE_DIRECTORY);
  163. file_prepare_directory($js_paths[0], FILE_CREATE_DIRECTORY);
  164. // Set the URI of the directory.
  165. $css_paths[1] = parse_url(file_create_url($css_paths[0]), PHP_URL_PATH);
  166. if (substr($css_paths[1], 0, strlen($GLOBALS['base_path'])) == $GLOBALS['base_path']) {
  167. $css_paths[1] = substr($css_paths[1], strlen($GLOBALS['base_path']));
  168. }
  169. $js_paths[1] = parse_url(file_create_url($js_paths[0]), PHP_URL_PATH);
  170. if (substr($js_paths[1], 0, strlen($GLOBALS['base_path'])) == $GLOBALS['base_path']) {
  171. $js_paths[1] = substr($js_paths[1], strlen($GLOBALS['base_path']));
  172. }
  173. }
  174. /**
  175. * Allow other modules to modify this aggregates contents.
  176. *
  177. * @param string $data
  178. * Raw CSS data.
  179. * @param array $files
  180. * List of files used to create this aggregate.
  181. * @param array $aggregate_settings
  182. * An associative array of hooks and settings used.
  183. *
  184. * @see advagg_get_css_aggregate_contents()
  185. * @see advagg_css_compress_advagg_get_css_aggregate_contents_alter()
  186. */
  187. function hook_advagg_get_css_aggregate_contents_alter(&$data, $files, $aggregate_settings) {
  188. if (empty($aggregate_settings['variables']['advagg_css_compressor'])) {
  189. return;
  190. }
  191. if ($aggregate_settings['variables']['advagg_css_compressor'] == 2) {
  192. advagg_css_compress_yui_cssmin($data);
  193. }
  194. }
  195. /**
  196. * Allow other modules to modify this aggregates contents.
  197. *
  198. * @param string $data
  199. * Raw JS data.
  200. * @param array $files
  201. * List of files used to create this aggregate.
  202. * @param array $aggregate_settings
  203. * An associative array of hooks and settings used.
  204. *
  205. * @see advagg_get_css_aggregate_contents()
  206. * @see advagg_css_compress_advagg_get_css_aggregate_contents_alter()
  207. */
  208. function hook_advagg_get_js_aggregate_contents_alter(&$data, $files, $aggregate_settings) {
  209. // Do nothing if js file compression is disabled.
  210. if (empty($aggregate_settings['variables']['advagg_js_compressor'])) {
  211. return;
  212. }
  213. // Compress it.
  214. $filename = drupal_hash_base64(serialize($files));
  215. advagg_js_compress_prep($data, $filename, $aggregate_settings, FALSE);
  216. }
  217. /**
  218. * Allow other modules to modify this files contents.
  219. *
  220. * @param string $contents
  221. * Raw file data.
  222. * @param string $file
  223. * Filename
  224. * @param array $aggregate_settings
  225. * An associative array of hooks and settings used.
  226. *
  227. * @see advagg_get_css_aggregate_contents()
  228. * @see advagg_css_compress_advagg_get_css_aggregate_contents_alter()
  229. */
  230. function hook_advagg_get_css_file_contents_alter(&$contents, $file, $aggregate_settings) {
  231. if (empty($aggregate_settings['variables']['advagg_css_compressor'])) {
  232. return;
  233. }
  234. if ($aggregate_settings['variables']['advagg_css_compressor'] == 2) {
  235. advagg_css_compress_yui_cssmin($contents);
  236. }
  237. }
  238. /**
  239. * Allow other modules to modify this files contents.
  240. *
  241. * @param string $contents
  242. * Raw file data.
  243. * @param string $file
  244. * Filename
  245. * @param array $aggregate_settings
  246. * An associative array of hooks and settings used.
  247. *
  248. * @see advagg_get_css_aggregate_contents()
  249. * @see advagg_css_compress_advagg_get_css_aggregate_contents_alter()
  250. */
  251. function hook_advagg_get_js_file_contents_alter(&$contents, $file, $aggregate_settings) {
  252. // Do nothing if js file compression is disabled.
  253. if (empty($aggregate_settings['variables']['advagg_js_compressor'])) {
  254. return;
  255. }
  256. // Make sure this file has been tested.
  257. $compressor = $aggregate_settings['variables']['advagg_js_compressor'];
  258. module_load_include('inc', 'advagg', 'advagg');
  259. $info = advagg_get_info_on_file($filename);
  260. if (!isset($info['advagg_js_compress'][$compressor]['code'])) {
  261. // Test file here on the spot.
  262. $info = advagg_js_compress_run_test($filename);
  263. }
  264. // Compress it if it passes the test.
  265. if (!empty($info['advagg_js_compress'][$compressor]['code']) && $info['advagg_js_compress'][$compressor]['code'] == 1) {
  266. advagg_js_compress_prep($contents, $filename, $aggregate_settings);
  267. }
  268. }
  269. /**
  270. * Allow other modules to modify $css_groups right before it is processed.
  271. *
  272. * @param array $css_groups
  273. * An associative array.
  274. * key - group
  275. * @param bool $preprocess_css
  276. * TRUE if preprocessing is enabled.
  277. *
  278. * @see _advagg_aggregate_css()
  279. * @see advagg_css_cdn_advagg_css_groups_alter()
  280. */
  281. function hook_advagg_css_groups_alter(&$css_groups, $preprocess_css) {
  282. // Work around a bug with seven_css_alter.
  283. // http://drupal.org/node/1937860
  284. $theme_keys[] = $GLOBALS['theme'];
  285. if (!empty($GLOBALS['base_theme_info'])) {
  286. foreach ($GLOBALS['base_theme_info'] as $base) {
  287. $theme_keys[] = $base->name;
  288. }
  289. }
  290. $match = FALSE;
  291. foreach ($theme_keys as $name) {
  292. if ($name == 'seven') {
  293. $match = TRUE;
  294. }
  295. }
  296. if (empty($match)) {
  297. return;
  298. }
  299. $target = FALSE;
  300. $last_group = FALSE;
  301. $last_key = FALSE;
  302. $kill_key = FALSE;
  303. $replaced = FALSE;
  304. foreach ($css_groups as $key => $group) {
  305. if (empty($target)) {
  306. if ($group['type'] == 'external' && $group['preprocess'] && $preprocess_css) {
  307. foreach ($group['items'] as $k => $value) {
  308. if ($value['data'] == 'themes/seven/jquery.ui.theme.css') {
  309. // Type should be file and not external (core bug).
  310. $value['type'] = 'file';
  311. $target = $value;
  312. unset($css_groups[$key]['items'][$k]);
  313. if (empty($css_groups[$key]['items'])) {
  314. unset($css_groups[$key]);
  315. $kill_key = $key;
  316. }
  317. }
  318. }
  319. }
  320. }
  321. else {
  322. $diff = array_merge(array_diff_assoc($group['browsers'], $target['browsers']), array_diff_assoc($target['browsers'], $group['browsers']));
  323. // @ignore sniffer_whitespace_openbracketspacing_openingwhitespace
  324. if ( $group['type'] != $target['type']
  325. || $group['group'] != $target['group']
  326. || $group['every_page'] != $target['every_page']
  327. || $group['media'] != $target['media']
  328. || $group['media'] != $target['media']
  329. || $group['preprocess'] != $target['preprocess']
  330. || !empty($diff)
  331. ) {
  332. if (!empty($last_group)) {
  333. $diff = array_merge(array_diff_assoc($last_group['browsers'], $target['browsers']), array_diff_assoc($target['browsers'], $last_group['browsers']));
  334. // @ignore sniffer_whitespace_openbracketspacing_openingwhitespace
  335. if ( $last_group['type'] != $target['type']
  336. || $last_group['group'] != $target['group']
  337. || $last_group['every_page'] != $target['every_page']
  338. || $last_group['media'] != $target['media']
  339. || $last_group['media'] != $target['media']
  340. || $last_group['preprocess'] != $target['preprocess']
  341. || !empty($diff)
  342. ) {
  343. // Insert New.
  344. $css_groups[$kill_key] = array(
  345. 'group' => $target['group'],
  346. 'type' => $target['type'],
  347. 'every_page' => $target['every_page'],
  348. 'media' => $target['media'],
  349. 'preprocess' => $target['preprocess'],
  350. 'browsers' => $target['browsers'],
  351. 'items' => array($target),
  352. );
  353. $replaced = TRUE;
  354. }
  355. else {
  356. // Insert above.
  357. $css_groups[$last_key]['items'][] = $target;
  358. $replaced = TRUE;
  359. }
  360. }
  361. }
  362. else {
  363. // Insert below.
  364. array_unshift($css_groups[$key]['items'], $target);
  365. $replaced = TRUE;
  366. }
  367. }
  368. $last_group = $group;
  369. $last_key = $key;
  370. if ($replaced) {
  371. break;
  372. }
  373. }
  374. ksort($css_groups);
  375. }
  376. /**
  377. * Allow other modules to modify $js_groups right before it is processed.
  378. *
  379. * @param array $js_groups
  380. * An associative array.
  381. * key - group
  382. * @param bool $preprocess_js
  383. * TRUE if preprocessing is enabled.
  384. *
  385. * @see _advagg_aggregate_js()
  386. * @see labjs_advagg_js_groups_alter()
  387. */
  388. function hook_advagg_js_groups_alter(&$js_groups, $preprocess_js) {
  389. if (!$preprocess_js) {
  390. return;
  391. }
  392. $labjs_location = labjs_get_path();
  393. foreach ($js_groups as &$data) {
  394. foreach ($data['items'] as &$values) {
  395. if ($values['data'] == $labjs_location) {
  396. // Strictly enforce preprocess = FALSE for labjs.
  397. $values['preprocess'] = FALSE;
  398. $data['preprocess'] = FALSE;
  399. break 2;
  400. }
  401. }
  402. }
  403. }
  404. /**
  405. * Allow other modules to modify $children & $elements before they are rendered.
  406. *
  407. * @param array $children
  408. * An array of children elements.
  409. * @param array $elements
  410. * A render array containing:
  411. * - #items: The CSS items as returned by drupal_add_css() and
  412. * altered by drupal_get_css().
  413. * - #group_callback: A function to call to group #items. Following
  414. * this function, #aggregate_callback is called to aggregate items within
  415. * the same group into a single file.
  416. * - #aggregate_callback: A function to call to aggregate the items within
  417. * the groups arranged by the #group_callback function.
  418. *
  419. * @see advagg_modify_css_pre_render()
  420. * @see advagg_css_compress_advagg_modify_css_pre_render_alter()
  421. */
  422. function hook_advagg_modify_css_pre_render_alter(&$children, &$elements) {
  423. // Get variables.
  424. $compressor = variable_get('advagg_css_inline_compressor', ADVAGG_CSS_INLINE_COMPRESSOR);
  425. // Do nothing if the compressor is disabled.
  426. if (empty($compressor)) {
  427. return;
  428. }
  429. // Do nothing if the page is not cacheable and inline compress if not
  430. // cacheable is not checked.
  431. if (!variable_get('advagg_css_inline_compress_if_not_cacheable', ADVAGG_CSS_INLINE_COMPRESS_IF_NOT_CACHEABLE) && !drupal_page_is_cacheable()) {
  432. return;
  433. }
  434. module_load_include('inc', 'advagg_css_compress', 'advagg_css_compress.advagg');
  435. if ($compressor == 2) {
  436. // Compress any inline CSS with YUI.
  437. foreach ($children as $key => &$values) {
  438. if (!empty($values['#value'])) {
  439. advagg_css_compress_yui_cssmin($values['#value']);
  440. }
  441. }
  442. }
  443. }
  444. /**
  445. * Allow other modules to modify $children & $elements before they are rendered.
  446. *
  447. * @param array $children
  448. * An array of children elements.
  449. * @param array $elements
  450. * A render array containing:
  451. * - #items: The JavaScript items as returned by drupal_add_js() and
  452. * altered by drupal_get_js().
  453. * - #group_callback: A function to call to group #items. Following
  454. * this function, #aggregate_callback is called to aggregate items within
  455. * the same group into a single file.
  456. * - #aggregate_callback: A function to call to aggregate the items within
  457. * the groups arranged by the #group_callback function.
  458. *
  459. * @see advagg_modify_js_pre_render()
  460. * @see advagg_js_compress_advagg_modify_js_pre_render_alter()
  461. */
  462. function hook_advagg_modify_js_pre_render_alter(&$children, &$elements) {
  463. // Get variables.
  464. $aggregate_settings['variables']['advagg_js_compressor'] = variable_get('advagg_js_inline_compressor', ADVAGG_JS_INLINE_COMPRESSOR);
  465. $aggregate_settings['variables']['advagg_js_max_compress_ratio'] = variable_get('advagg_js_max_compress_ratio', ADVAGG_JS_MAX_COMPRESS_RATIO);
  466. // Do nothing if the compressor is disabled.
  467. if (empty($aggregate_settings['variables']['advagg_js_compressor'])) {
  468. return;
  469. }
  470. // Do nothing if the page is not cacheable and inline compress if not
  471. // cacheable is not checked.
  472. if (!variable_get('advagg_js_inline_compress_if_not_cacheable', ADVAGG_JS_INLINE_COMPRESS_IF_NOT_CACHEABLE) && !drupal_page_is_cacheable()) {
  473. return;
  474. }
  475. // Compress any inline JS.
  476. module_load_include('inc', 'advagg_js_compress', 'advagg_js_compress.advagg');
  477. foreach ($children as $key => &$values) {
  478. if (!empty($values['#value'])) {
  479. $contents = $values['#value'];
  480. $filename = drupal_hash_base64($contents);
  481. advagg_js_compress_prep($contents, $filename, $aggregate_settings, FALSE);
  482. $values['#value'] = $contents;
  483. }
  484. }
  485. }
  486. /**
  487. * Allow other modules to modify $css_groups right before it is processed.
  488. *
  489. * @param array $original
  490. * array of original settings.
  491. * @param array $aggregate_settings
  492. * array of contextual settings.
  493. * @param int $mode
  494. * 0 to change context to what is inside of $aggregate_settings.
  495. * 1 to change context back.
  496. *
  497. * @see advagg_context_css()
  498. * @see advagg_advagg_context_alter()
  499. */
  500. function hook_advagg_context_alter(&$original, $aggregate_settings, $mode) {
  501. if ($mode == 0) {
  502. // Change context.
  503. $original['base_root'] = $GLOBALS['base_root'];
  504. $original['base_url'] = $GLOBALS['base_url'];
  505. $original['base_path'] = $GLOBALS['base_path'];
  506. $original['is_https'] = $GLOBALS['is_https'];
  507. $GLOBALS['is_https'] = $aggregate_settings['variables']['is_https'];
  508. if ($aggregate_settings['variables']['is_https']) {
  509. $GLOBALS['base_root'] = str_replace('http://', 'https://', $GLOBALS['base_root']);
  510. $GLOBALS['base_url'] = str_replace('http://', 'https://', $GLOBALS['base_url']);
  511. }
  512. else {
  513. $GLOBALS['base_root'] = str_replace('https://', 'http://', $GLOBALS['base_root']);
  514. $GLOBALS['base_url'] = str_replace('https://', 'http://', $GLOBALS['base_url']);
  515. }
  516. $GLOBALS['base_path'] = $aggregate_settings['variables']['base_path'];
  517. }
  518. elseif ($mode == 1) {
  519. // Change context back.
  520. if (isset($original['base_root'])) {
  521. $GLOBALS['base_root'] = $original['base_root'];
  522. }
  523. if (isset($original['base_url'])) {
  524. $GLOBALS['base_url'] = $original['base_url'];
  525. }
  526. if (isset($original['base_path'])) {
  527. $GLOBALS['base_path'] = $original['base_path'];
  528. }
  529. if (isset($original['is_https'])) {
  530. $GLOBALS['is_https'] = $original['is_https'];
  531. }
  532. }
  533. }
  534. /**
  535. * @} End of "addtogroup hooks".
  536. */
Error | ELMSLN API

Error

×

Error message

  • Warning: Cannot modify header information - headers already sent by (output started at /var/www/html/elmsln_community/api.elmsln.org/includes/common.inc:2791) in drupal_send_headers() (line 1499 of /var/www/html/elmsln_community/api.elmsln.org/includes/bootstrap.inc).
  • Error: Call to undefined function apc_delete() in DrupalAPCCache->clear() (line 289 of /var/www/html/elmsln_community/api.elmsln.org/sites/all/modules/apc/drupal_apc_cache.inc).
The website encountered an unexpected error. Please try again later.