PageRenderTime 45ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-content/plugins/bp-group-documents/include/bp_group_documents_functions.php

https://bitbucket.org/sajjadbagwan/ncsha
PHP | 207 lines | 99 code | 31 blank | 77 comment | 11 complexity | 2d56d3ca65ec655d4ef6894f1605dab6 MD5 | raw file
Possible License(s): GPL-3.0, LGPL-3.0, GPL-2.0, MIT
  1. <?php
  2. // Exit if accessed directly
  3. if ( !defined('ABSPATH') ) {
  4. exit;
  5. }
  6. /**
  7. * @since version 0.5
  8. * containes functions previous on index.php
  9. */
  10. /**
  11. * Registers the plugin's template directory.
  12. *
  13. * @since 1.12
  14. */
  15. function bp_group_documents_register_template_stack() {
  16. bp_register_template_stack( 'bp_group_documents_template_directory', 20 );
  17. }
  18. add_action( 'bp_actions', 'bp_group_documents_register_template_stack', 0 );
  19. /**
  20. * Returns the directory containing the default templates for the plugin.
  21. *
  22. * @since 1.12
  23. *
  24. * @return string
  25. */
  26. function bp_group_documents_template_directory() {
  27. return WP_PLUGIN_DIR . '/' . BP_GROUP_DOCUMENTS_DIR . '/templates';
  28. }
  29. /**
  30. * bp_group_documents_display()
  31. *
  32. * Loads the template part for the primary group display.
  33. *
  34. * version 2.0 7/3/2013 lenasterg
  35. */
  36. function bp_group_documents_display() {
  37. bp_get_template_part( 'groups/single/documents' );
  38. }
  39. /**
  40. *
  41. * @version 2.0, 13/5/2013, lenasterg
  42. */
  43. function bp_group_documents_display_header() {
  44. $nav_page_name = get_option('bp_group_documents_nav_page_name');
  45. $name = !empty($nav_page_name) ? $nav_page_name : __('Documents' , 'bp-group-documents');
  46. _e('Group') . ' ' . $name;
  47. }
  48. /**
  49. *
  50. */
  51. function bp_group_documents_display_title() {
  52. echo get_option('bp_group_documents_nav_page_name') . ' ' . __('List' , 'bp-group-documents');
  53. }
  54. /* * ***********************************************************************
  55. * **********************EVERYTHING ELSE************************************
  56. * *********************************************************************** */
  57. /**
  58. * bp_group_documents_delete()
  59. *
  60. * After perfoming several validation checks, deletes both the uploaded
  61. * file and the reference in the database
  62. */
  63. function bp_group_documents_delete($id) {
  64. //check nonce
  65. if ( !wp_verify_nonce($_REQUEST['_wpnonce'] , 'group-documents-delete-link') ) {
  66. bp_core_add_message(__('There was a security problem' , 'bp-group-documents') , 'error');
  67. return false;
  68. }
  69. if ( !ctype_digit($id) ) {
  70. bp_core_add_message(__('The item to delete could not be found' , 'bp-group-documents') , 'error');
  71. return false;
  72. }
  73. $document = new BP_Group_Documents($id);
  74. if ( $document->current_user_can('delete') ) {
  75. if ( $document->delete() ) {
  76. do_action('bp_group_documents_delete_success' , $document);
  77. return true;
  78. }
  79. }
  80. return false;
  81. }
  82. /**
  83. * bp_group_documents_check_ext()
  84. *
  85. * checks whether the passed filename ends in an extension
  86. * that is allowed by the site admin
  87. */
  88. function bp_group_documents_check_ext($filename) {
  89. if ( !$filename ) {
  90. return false;
  91. }
  92. $valid_formats_string = get_option('bp_group_documents_valid_file_formats');
  93. $valid_formats_array = explode(',' , $valid_formats_string);
  94. $extension1 = substr($filename , (strrpos($filename , ".") + 1));
  95. $extension = strtolower($extension1);
  96. if ( in_array($extension , $valid_formats_array) ) {
  97. return true;
  98. }
  99. return false;
  100. }
  101. /**
  102. * get_file_size()
  103. *
  104. * returns a human-readable file-size for the passed file
  105. * adapted from a function in the PHP manual comments
  106. */
  107. function get_file_size($document , $precision = 1) {
  108. $units = array('b' , 'k' , 'm' , 'g');
  109. $bytes1 = file_exists($document->get_path(1)) ? filesize($document->get_path(1)) : 0;
  110. $bytes = max($bytes1 , 0);
  111. $pow1 = floor(($bytes ? log($bytes) : 0) / log(1024));
  112. $pow = min($pow1 , count($units) - 1);
  113. $bytes /= pow(1024 , $pow);
  114. return round($bytes , $precision) . $units[$pow];
  115. }
  116. /**
  117. * return_bytes()
  118. *
  119. * taken from the PHP manual examples. Returns the number of bites
  120. * when given an abrevition (eg, max_upload_size)
  121. */
  122. function return_bytes($val) {
  123. $val = trim($val);
  124. $last = strtolower($val[strlen($val) - 1]);
  125. switch ($last) {
  126. // The 'G' modifier is available since PHP 5.1.0
  127. case 'g':
  128. $val *= 1024;
  129. case 'm':
  130. $val *= 1024;
  131. case 'k':
  132. $val *= 1024;
  133. }
  134. return $val;
  135. }
  136. /**
  137. * bp_group_documents_remove_data()
  138. *
  139. * Cleans out both the files and the database records when a group is deleted
  140. */
  141. function bp_group_documents_remove_data($group_id) {
  142. $results = BP_Group_Documents::get_list_by_group($group_id);
  143. if ( count($results) >= 1 ) {
  144. foreach ( $results as $document_params ) {
  145. $document = new BP_Group_Documents($document_params['id'] , $document_params);
  146. $document->delete();
  147. do_action('bp_group_documents_delete_with_group' , $document);
  148. }
  149. }
  150. }
  151. add_action('groups_group_deleted' , 'bp_group_documents_remove_data');
  152. /**
  153. * bp_group_documents_register_taxonomies()
  154. *
  155. * registers the taxonomies to use with the Wordpress Custom Taxonomy API
  156. */
  157. function bp_group_documents_register_taxonomies() {
  158. register_taxonomy('group-documents-category' , 'group-document' , array('hierarchical' => true , 'label' => __('Group Document Categories' , 'bp-group-documents') , 'query_var' => false));
  159. }
  160. add_action('init' , 'bp_group_documents_register_taxonomies');
  161. /**
  162. * bp_group_document_set_cookies()
  163. *
  164. * Set any cookies for our component. This will usually be for list filtering and sorting.
  165. * We must create a dedicated function for this, to fire before the headers are sent
  166. * (doing this in the template object with the rest of the filtering/sorting is too late)
  167. */
  168. function bp_group_documents_set_cookies() {
  169. if ( isset($_GET['order']) ) {
  170. setcookie('bp-group-documents-order' , $_GET['order'] , time() + 60 * 60 + 24); //expires in one day
  171. }
  172. if ( isset($_GET['category']) ) {
  173. setcookie('bp-group-documents-category' , $_GET['category'] , time() + 60 * 60 * 24);
  174. }
  175. }
  176. add_action('wp' , 'bp_group_documents_set_cookies');