Advertisement
maxworkingwell

Toolset custom functions

Jan 14th, 2022 (edited)
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 8.67 KB | None | 0 0
  1. // // create download_post on submit form on page https://drama-otaku.com/add-subtitle-project/
  2. // add_action('cred_save_data', 'download_post_data_action',10,2);
  3. // function download_post_data_action($post_id, $form_data){
  4.  
  5. // $forms = array( 41833 );
  6. // if (in_array($form_data['id'], $forms)){
  7.  
  8. // $files = get_post_meta( $post_id, 'wpcf-upload-subtitle' );
  9. // // $sort_order_repeating = get_post_meta($post_id, '_wpcf-field-repeating-sort-order', true);
  10.    
  11. // foreach ($files as $file) {
  12. // $single_value = $file;
  13. // //Replace url to directory path
  14. // $path = str_replace( site_url('/'), ABSPATH, esc_url( $file) );
  15.  
  16. // if ( is_file( $path ) ){
  17. // $filename = basename($path);
  18. // $filesize = size_format( filesize( $path ) );
  19.  
  20. // // Create post object
  21. // $download_post = array(
  22. // 'post_title' => get_the_title( $post_id ),
  23. // 'post_status' => 'publish',
  24. // 'post_type' => 'download'
  25.    
  26. // );
  27.  
  28. // $new_post_id = wp_insert_post( $download_post );
  29. // add_post_meta( $new_post_id, 'wpcf-download-file', $single_value );
  30. // // update_post_meta( $new_post_id, '_wpcf-field-repeating-sort-order', $sort_order_repeating);  
  31. // update_post_meta( $new_post_id, 'wpcf-download-counts', '0');
  32. // update_post_meta( $new_post_id, 'wpcf-filename', $filename);
  33. // update_post_meta( $new_post_id, 'wpcf-filesize', $filesize);
  34. // toolset_connect_posts( 'subtitle-download', $post_id, $new_post_id );
  35.  
  36. // }
  37. // }
  38.  
  39. // }
  40. // }
  41.  
  42. // // edit subtitle
  43. // add_action('cred_submit_complete', 'subtitle_edit_post_data_action',10,2);
  44. // function subtitle_edit_post_data_action($post_id, $form_data){
  45. // $forms = array( 42138 );
  46. // if (in_array($form_data['id'], $forms)){
  47.  
  48. // // Array Downloads
  49. // $downloads = toolset_get_related_posts(
  50. // get_the_ID(), // get posts related to the current post (subtitle)
  51. // 'subtitle-download', // relationship between the posts
  52. // 'parent', // get posts where subtitle is the parent in given relationship
  53. // 10, 0, // pagination
  54. // array(), // no extra args needed here
  55. // 'post_id', // return post ids
  56. // 'child' // return the related child post(s) in this relationship
  57. // );
  58.  
  59. // // Array Downloads
  60. // $downloadFiles = array();   
  61. // foreach ($downloads as $download ) {
  62. // $downloadFiles[] = get_post_meta($download, 'wpcf-download-file', true);
  63. // }
  64. // // print_r($downloadFiles);
  65.  
  66.    
  67. // // Array Files
  68. // $files = get_post_meta( $post_id, 'wpcf-upload-subtitle' );
  69. // // print_r($subtitleFiles);
  70.    
  71. // $array1 = $files;
  72. // $array2 = $downloadFiles;
  73.    
  74. // // insert  download 
  75. // $resultsInsert = array_diff($array1, $array2);
  76. // // delete  download 
  77. // $resultsDelete = array_diff($array2, $array1);
  78. // // print_r($results);
  79.  
  80. // if (!empty($resultsInsert) || empty($downloads)) {
  81.    
  82. // foreach ($resultsInsert as $resultInsert) {
  83. // $single_value = $resultInsert;
  84.  
  85. // //Replace url to directory path
  86. // $path = str_replace( site_url('/'), ABSPATH, esc_url( $resultInsert) );
  87. // $filename = basename($path);
  88. // $filesize = size_format( filesize( $path ) );
  89.  
  90. // // Create post object
  91. // $download_post = array(
  92. // 'post_title' => get_the_title( $post_id ),
  93. // 'post_status' => 'publish',
  94. // 'post_type' => 'download'
  95. // );
  96.  
  97. // $new_post_id = wp_insert_post( $download_post );
  98. // add_post_meta( $new_post_id, 'wpcf-download-file', $single_value );
  99. // update_post_meta( $new_post_id, 'wpcf-download-counts', '0');
  100. // update_post_meta( $new_post_id, 'wpcf-filename', $filename);
  101. // update_post_meta( $new_post_id, 'wpcf-filesize', $filesize);
  102. // toolset_connect_posts( 'subtitle-download', $post_id, $new_post_id );
  103.  
  104. // }
  105. // }
  106.    
  107. // if (!empty($resultsDelete)) {
  108.    
  109. // $query = new WP_Query(
  110. //     array(
  111. //         'post_type' => 'download', //Child post type slug
  112. //         'numberposts' => -1,
  113. //         'toolset_relationships' => array(
  114. //             'role' => 'child',
  115. //             'related_to' => $post_id, // ID of starting post
  116. //             'relationship' =>'subtitle-download',
  117. //         ),
  118. //     'meta_query' => array(
  119. //         array(
  120. //             'key'     => 'wpcf-download-file',
  121. //             'value'   => $resultsDelete,
  122. //             'compare' => 'IN',
  123. //         ),
  124. //     ),
  125. //     )
  126. // );
  127. // $related_download_posts = $query->posts;
  128. // // Loop
  129. // if ( $query->have_posts() ) {
  130. //  while ( $query->have_posts() ) {
  131. //      $query->the_post();
  132. //      wp_delete_post( get_the_ID(), true );
  133. //  }
  134. // }
  135. // wp_reset_postdata();
  136.    
  137. // }
  138.    
  139. // }
  140. // }
  141.  
  142.  
  143.  
  144.  
  145. // // Redirect to download file
  146. // add_filter('cred_success_redirect', 'custom_download_redirect',10,3);
  147. // function custom_download_redirect($url, $post_id, $form_data){
  148. //     if ($form_data['id']==42521) {
  149. //  $url = get_post_meta($post_id, 'wpcf-download-file', true);
  150. //     }
  151. //     return $url;
  152. // }
  153.  
  154. // // +1 downloads
  155. // add_action('cred_save_data', 'download_counter_data_action',10,2);
  156. // function download_counter_data_action($post_id, $form_data){
  157.  
  158. // if ($form_data['id']==42521){
  159. // $counter = get_post_meta($post_id, 'wpcf-download-counts', true);
  160. // $counter_increment = $counter + 1;
  161. // update_post_meta( $post_id, 'wpcf-download-counts', $counter_increment);
  162. // }
  163. // }
  164.  
  165.  
  166.  
  167. /* Cleanup files from database and server upon submitting specific subtitle cred form - edit subtitle */
  168. add_action('cred_save_data','images_cleanup', 10, 2);
  169. function images_cleanup($post_id, $form_data) {
  170.     # if a specific form or two..
  171.    $forms = array( 42138 );
  172. if (in_array($form_data['id'], $forms)){
  173.         $type = get_post_type($post_id, $form_data);
  174.         # if a specific post type..
  175.        if ($type == 'subtitle-project') {
  176.             global $wpdb;
  177.             # Compare both tables and remove duplicates while listing only the ID from wp_post as attachment
  178.            $allposts = $wpdb->get_results( $wpdb->prepare("SELECT id FROM wp6y_posts WHERE post_parent=$post_id AND post_type ='attachment' AND guid NOT IN (SELECT meta_value FROM wp6y_postmeta WHERE post_id = $post_id AND meta_key LIKE 'wpcf-upload-subtitle' OR meta_key LIKE '_wp_attached_file')") );
  179.             foreach ($allposts as $singlepost) {
  180.                 $post_id_n1=$singlepost->id;
  181.                 wp_delete_attachment($post_id_n1, true);
  182.             } # END global $wpdb;
  183.        } # END specific post type
  184.    } # END specific form
  185.  } # END function images_cleanup
  186.  
  187.  
  188. // 2. Function display attachment in different ways
  189. add_shortcode('display-file-size', function($atts){
  190.    global $post;
  191.     $atts = shortcode_atts( array(
  192.         'file_url' =>'' ,
  193.         'meta_name' => 'name',
  194.         'meta_name' => 'size',
  195.         'meta_name' => 'caption',
  196.         'meta_name' => 'description',
  197.         'meta_name' => 'title',
  198.         'meta_name' => 'ext',
  199.         'meta_name' => 'count',
  200.     ), $atts );
  201.        
  202.     $res = '';
  203.    
  204.    $attachment_id = attachment_url_to_postid($atts['file_url']);
  205.    $file = get_attached_file($attachment_id);
  206.    $attachment = get_post($attachment_id);
  207.    $filetype = wp_check_filetype( $file );
  208.        
  209.     switch($atts['meta_name']){
  210.           case 'name':
  211.               $fileparts = explode('/', $file);
  212.               $res = end($fileparts);
  213.             break;
  214.     }
  215.     switch($atts['meta_name']){
  216.           case 'size':
  217.             $res = size_format(filesize( $file ));
  218.             break;
  219.     }
  220.     switch($atts['meta_name']){
  221.           case 'caption':
  222.             $res = wp_get_attachment_caption($attachment_id);
  223.             break;
  224.     }
  225.     switch($atts['meta_name']){
  226.           case 'description':
  227.             $res = $attachment->post_content;
  228.             break;
  229.     }
  230.     switch($atts['meta_name']){
  231.           case 'title':
  232.             $res = get_the_title($attachment_id);
  233.             break;
  234.     }
  235.     switch($atts['meta_name']){
  236.           case 'ext':
  237.             $res = $filetype['ext'];
  238.             break;
  239.     }
  240.     switch($atts['meta_name']){
  241.           case 'count':
  242.             $res = get_post_meta($attachment_id, 'wpcf-download-counts', true);
  243.             break;
  244.     }
  245.     return $res;
  246. });
  247.  
  248.  
  249. // 2 function +1 downloads and update modified date
  250. add_action('cred_save_data', 'download_counter_data_action', 10, 2 );
  251. function download_counter_data_action($post_id, $form_data){
  252. if ($form_data['id']==42521){
  253. global $wpdb;
  254. $date = get_the_date( 'Y-m-d H:i:s', $post_id );
  255. $post_modified = $date;
  256. $post_modified_gmt = get_gmt_from_date($date);
  257. $wpdb->query("UPDATE $wpdb->posts SET post_modified = '{$post_modified}', post_modified_gmt = '{$post_modified_gmt}'  WHERE ID = {$post_id}" );
  258. $attachment_id = attachment_url_to_postid($_POST['file']);
  259. $counter = get_post_meta($attachment_id, 'wpcf-download-counts', true);
  260. $counter_increment = $counter + 1;
  261. update_post_meta( $attachment_id, 'wpcf-download-counts', $counter_increment);
  262. }
  263. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement