functions.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  1. <?php
  2. /*
  3. |--------------------------------------------------------------------------
  4. | Register The Auto Loader
  5. |--------------------------------------------------------------------------
  6. |
  7. | Composer provides a convenient, automatically generated class loader for
  8. | our theme. We will simply require it into the script here so that we
  9. | don't have to worry about manually loading any of our classes later on.
  10. |
  11. */
  12. if (! file_exists($composer = __DIR__.'/vendor/autoload.php')) {
  13. wp_die(__('Error locating autoloader. Please run <code>composer install</code>.', 'sage'));
  14. }
  15. require $composer;
  16. /*
  17. |--------------------------------------------------------------------------
  18. | Register The Bootloader
  19. |--------------------------------------------------------------------------
  20. |
  21. | The first thing we will do is schedule a new Acorn application container
  22. | to boot when WordPress is finished loading the theme. The application
  23. | serves as the "glue" for all the components of Laravel and is
  24. | the IoC container for the system binding all of the various parts.
  25. |
  26. */
  27. if (! function_exists('\Roots\bootloader')) {
  28. wp_die(
  29. __('You need to install Acorn to use this theme.', 'sage'),
  30. '',
  31. [
  32. 'link_url' => 'https://roots.io/acorn/docs/installation/',
  33. 'link_text' => __('Acorn Docs: Installation', 'sage'),
  34. ]
  35. );
  36. }
  37. \Roots\bootloader()->boot();
  38. /*
  39. |--------------------------------------------------------------------------
  40. | Register Sage Theme Files
  41. |--------------------------------------------------------------------------
  42. |
  43. | Out of the box, Sage ships with categorically named theme files
  44. | containing common functionality and setup to be bootstrapped with your
  45. | theme. Simply add (or remove) files from the array below to change what
  46. | is registered alongside Sage.
  47. |
  48. */
  49. collect(['setup', 'filters'])
  50. ->each(function ($file) {
  51. if (! locate_template($file = "app/{$file}.php", true, true)) {
  52. wp_die(
  53. /* translators: %s is replaced with the relative file path */
  54. sprintf(__('Error locating <code>%s</code> for inclusion.', 'sage'), $file)
  55. );
  56. }
  57. });
  58. //ajout des champs personnalisés Minutage et Images; pour les afficher rdv dans le fichier content-single
  59. //le faire qu'une suele fois et après mettre en commentaire
  60. // function ajout_champs() {
  61. // add_post_meta(get_the_ID(), 'Index', true);
  62. // add_post_meta(get_the_ID(), 'Minutage', true);
  63. // add_post_meta(get_the_ID(), 'Images', true);
  64. // add_post_meta(get_the_ID(), 'BandeSon', true);
  65. // add_post_meta(get_the_ID(), 'Ecrits', true);
  66. // add_post_meta(get_the_ID(), 'VoixOffIn', true);
  67. // add_post_meta(get_the_ID(), 'isMainPart', true);
  68. // add_post_meta(get_the_ID(), 'isSubPart', true);
  69. // }
  70. // add_action('init', 'ajout_champs');
  71. function cacher_bloc_head_admin() {
  72. // Cache le bloc head du plugin de PublicPress
  73. echo '<style>.pp-version-notice-bold-purple { display: none; }</style>';
  74. echo '<style>.notice-warning { display: none; }</style>';
  75. }
  76. add_action('admin_head', 'cacher_bloc_head_admin');
  77. function cacher_bloc_foot_admin() {
  78. // Cache le bloc foot du plugin de PublicPress
  79. echo '<style>.pp-rating { display: none; }</style>';
  80. echo '<style>.pp-pressshack-logo { display: none; }</style>';
  81. echo '<style> footer nav{ display: none; }</style>';
  82. echo '<style> ul.subsubsub li:nth-child(1) a { display: none;}</style>';
  83. }
  84. add_action('admin_footer', 'cacher_bloc_foot_admin');
  85. function custom_filter_dropdown() {
  86. global $typenow;
  87. if ($typenow == 'post') { // Adjust post type if needed
  88. $selected = isset($_GET['custom_field_filter']) ? $_GET['custom_field_filter'] : '';
  89. $options = array(
  90. 'isMainPart' => __('isMainPart', 'textdomain'),
  91. 'isSubPart' => __('isSubPart', 'textdomain'),
  92. );
  93. echo '<select name="custom_field_filter">';
  94. echo '<option value="">' . __('All Custom Fields', 'textdomain') . '</option>';
  95. foreach ($options as $key => $label) {
  96. echo '<option value="' . $key . '"' . selected($selected, $key, false) . '>' . $label . '</option>';
  97. }
  98. echo '</select>';
  99. }
  100. }
  101. add_action('restrict_manage_posts', 'custom_filter_dropdown');
  102. function filter_by_custom_field($query) {
  103. global $pagenow;
  104. if (is_admin() && $pagenow == 'edit.php' && isset($_GET['custom_field_filter'])) {
  105. $custom_field_key = sanitize_text_field($_GET['custom_field_filter']);
  106. if (!empty($custom_field_key)) {
  107. $meta_query = array(
  108. array(
  109. 'key' => $custom_field_key,
  110. 'compare' => 'EXISTS',
  111. ),
  112. );
  113. $query->query_vars['meta_query'] = $meta_query;
  114. }
  115. }
  116. }
  117. add_filter('parse_query', 'filter_by_custom_field');
  118. add_action('wp', 'generate_csv');
  119. function generate_csv() {
  120. if ('csv' === (isset($_GET['format']) ? $_GET['format'] : null)) {
  121. // Query to get all posts
  122. $args = array(
  123. 'post_type' => 'post',
  124. 'posts_per_page' => -1,
  125. 'meta_key' => 'index',
  126. 'meta_type' => 'NUMERIC',
  127. 'orderby' => 'meta_value',
  128. 'order' => 'ASC'
  129. );
  130. $posts = get_posts($args);
  131. if (!empty($posts)) {
  132. // Initialize CSV data array
  133. $csvData = array();
  134. // Add CSV header row
  135. $csvData[] = array('Temps', 'Images', 'Voix Off et In', 'Bande Son', 'Écrits', 'Section');
  136. // Loop through posts and add them to the CSV data array
  137. foreach ($posts as $post) {
  138. $post_content = wp_strip_all_tags($post->post_content);
  139. $content_array = explode("---", $post_content);
  140. $temps = $post->post_title;
  141. $images = isset($content_array[1]) ? $content_array[1] : '';
  142. $images = substr($images, 0, -15);
  143. $voixOffIn = isset($content_array[2]) ? $content_array[2] : '';
  144. $voixOffIn = substr($voixOffIn, 0, -10);
  145. $bandeson = isset($content_array[3]) ? $content_array[3] : '';
  146. $bandeson = substr($bandeson, 0, -8);
  147. $ecrits = isset($content_array[4]) ? $content_array[4] : '';
  148. if (get_post_meta($post->ID, 'isMainPart', true)) {
  149. $section = get_post_meta($post->ID, 'isMainPart', true);
  150. } elseif (get_post_meta($post->ID, 'isSubPart', true)) {
  151. $section = get_post_meta($post->ID, 'isSubPart', true);
  152. } else {
  153. $section = '';
  154. }
  155. $csvData[] = array(
  156. $temps,
  157. $images,
  158. $voixOffIn,
  159. $bandeson,
  160. $ecrits,
  161. $section,
  162. );
  163. }
  164. $csvFileName = 'partition_livre_d_image.csv';
  165. header('Content-Encoding: UTF-8');
  166. header('Content-Type: text/csv; charset=utf-8');
  167. header('Content-Disposition: attachment; filename=' . $csvFileName);
  168. echo "\xEF\xBB\xBF"; // UTF-8 BOM
  169. // Output CSV data
  170. $output = fopen('php://output', 'w');
  171. foreach ($csvData as $row) {
  172. fputcsv($output, $row);
  173. }
  174. fclose($output);
  175. exit;
  176. }
  177. }
  178. }
  179. function custom_admin_styles() {
  180. wp_enqueue_style('custom-admin', get_template_directory_uri() . '/resources/styles/admin_custom.css');
  181. wp_enqueue_script('custom-admin-script', get_template_directory_uri() . '/resources/scripts/custom-admin-script.js', array(), null, true);
  182. }
  183. add_action('admin_enqueue_scripts', 'custom_admin_styles');
  184. function restrict_revisions_by_author($query) {
  185. global $current_user;
  186. // Check if the user is logged in
  187. if (is_user_logged_in()) {
  188. get_currentuserinfo();
  189. $author_id = $current_user->ID;
  190. // Modify the query to filter revisions by author
  191. if (isset($query->query_vars['post_type']) && $query->query_vars['post_type'] == 'revision') {
  192. $query->set('author', $author_id);
  193. }
  194. }
  195. }
  196. add_action('pre_get_posts', 'restrict_revisions_by_author');
  197. function customize_editor_for_editor_role() {
  198. if (current_user_can('revisor')) {
  199. wp_enqueue_script('custom-editor-script', get_template_directory_uri() . '/resources/scripts/custom-editor-script.js', array(), null, true);
  200. wp_enqueue_style('custom-editor-style', get_template_directory_uri() . '/resources/styles/custom-editor-style.css');
  201. // Create my revisions page
  202. $current_user = wp_get_current_user();
  203. $author_id = $current_user->ID;
  204. $revisionary_page = admin_url('admin.php?page=revisionary-q&author=' . $author_id);
  205. // Get the content of the specific page (replace '123' with the actual page ID)
  206. $page_id = 13940;
  207. $page_content = get_post_field('post_content', $page_id);
  208. $website_url = site_url();
  209. // Localize the page content to be accessible in your custom script
  210. wp_localize_script('custom-editor-script', 'pageContentData', array(
  211. 'content' => $page_content,
  212. 'websiteUrl' => $website_url,
  213. 'myRevisionsUrl' => $revisionary_page,
  214. ));
  215. }
  216. }
  217. add_action('admin_enqueue_scripts', 'customize_editor_for_editor_role');
  218. function remove_roles() {
  219. remove_role('author');
  220. remove_role('editor');
  221. remove_role('subscriber');
  222. remove_role('contributor');
  223. }
  224. add_action('init', 'remove_roles');
  225. // Redirect all post URLs to the homepage
  226. function redirect_single_posts_to_homepage() {
  227. if (is_single() && isset($_GET['p'])) {
  228. wp_redirect(home_url(), 301);
  229. exit();
  230. }
  231. }
  232. add_action('template_redirect', 'redirect_single_posts_to_homepage');
  233. function log_posts_with_user_revisions() {
  234. $current_user = wp_get_current_user();
  235. if (current_user_can('revisor')) {
  236. if ($current_user instanceof WP_User) {
  237. $current_user_id = $current_user->ID;
  238. $args = array(
  239. 'post_type' => 'revision', // Fetch all post types including revisions
  240. 'post_status' => 'inherit', // Fetch all post statuses including revisions
  241. 'posts_per_page' => -1, // Retrieve all posts
  242. 'author' => $current_user_id,
  243. 'fields' => 'ids', // Retrieve only IDs to speed up the query
  244. );
  245. $all_posts = get_posts($args);
  246. $parents_posts_ids = array();
  247. $revisions_dates = array();
  248. foreach ($all_posts as $post_id) {
  249. $current_revision_date = array();
  250. $current_revision_date[] = get_post(wp_get_post_parent_id($post_id))->post_title;
  251. $current_revision_date[] = get_post($post_id)->post_date;
  252. $revisions_dates[] = $current_revision_date;
  253. $parents_posts_ids[] = wp_get_post_parent_id($post_id);
  254. }
  255. $parents_posts_ids = array_unique($parents_posts_ids);
  256. $history_posts_titles = array();
  257. $history_posts_contents = array();
  258. foreach ($parents_posts_ids as $parent_post_id) {
  259. $history_posts_titles[] = get_post($parent_post_id)->post_title;
  260. $history_posts_contents[] = strip_tags(get_post($parent_post_id)->post_content);
  261. }
  262. // Enqueue the script
  263. wp_enqueue_script('contribution-history-script', get_template_directory_uri() . '/resources/scripts/contribution_history.js', array('jquery'), '1.0', true);
  264. // Localize script to pass data to JavaScript
  265. wp_localize_script('contribution-history-script', 'historyData', array(
  266. 'titles' => $history_posts_titles,
  267. 'contents' => $history_posts_contents,
  268. 'dates' => $revisions_dates
  269. ));
  270. }
  271. }
  272. }
  273. add_action('admin_enqueue_scripts', 'log_posts_with_user_revisions');
  274. // function supprimer_tous_les_articles() {
  275. // $args = array(
  276. // 'post_type' => 'post',
  277. // 'posts_per_page' => -1,
  278. // );
  279. // $posts = get_posts($args);
  280. // foreach ($posts as $post) {
  281. // wp_delete_post($post->ID, true);
  282. // }
  283. // }
  284. // add_action('init', 'supprimer_tous_les_articles');