Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // // create download_post on submit form on page https://drama-otaku.com/add-subtitle-project/
- // add_action('cred_save_data', 'download_post_data_action',10,2);
- // function download_post_data_action($post_id, $form_data){
- // $forms = array( 41833 );
- // if (in_array($form_data['id'], $forms)){
- // $files = get_post_meta( $post_id, 'wpcf-upload-subtitle' );
- // // $sort_order_repeating = get_post_meta($post_id, '_wpcf-field-repeating-sort-order', true);
- // foreach ($files as $file) {
- // $single_value = $file;
- // //Replace url to directory path
- // $path = str_replace( site_url('/'), ABSPATH, esc_url( $file) );
- // if ( is_file( $path ) ){
- // $filename = basename($path);
- // $filesize = size_format( filesize( $path ) );
- // // Create post object
- // $download_post = array(
- // 'post_title' => get_the_title( $post_id ),
- // 'post_status' => 'publish',
- // 'post_type' => 'download'
- // );
- // $new_post_id = wp_insert_post( $download_post );
- // add_post_meta( $new_post_id, 'wpcf-download-file', $single_value );
- // // update_post_meta( $new_post_id, '_wpcf-field-repeating-sort-order', $sort_order_repeating);
- // update_post_meta( $new_post_id, 'wpcf-download-counts', '0');
- // update_post_meta( $new_post_id, 'wpcf-filename', $filename);
- // update_post_meta( $new_post_id, 'wpcf-filesize', $filesize);
- // toolset_connect_posts( 'subtitle-download', $post_id, $new_post_id );
- // }
- // }
- // }
- // }
- // // edit subtitle
- // add_action('cred_submit_complete', 'subtitle_edit_post_data_action',10,2);
- // function subtitle_edit_post_data_action($post_id, $form_data){
- // $forms = array( 42138 );
- // if (in_array($form_data['id'], $forms)){
- // // Array Downloads
- // $downloads = toolset_get_related_posts(
- // get_the_ID(), // get posts related to the current post (subtitle)
- // 'subtitle-download', // relationship between the posts
- // 'parent', // get posts where subtitle is the parent in given relationship
- // 10, 0, // pagination
- // array(), // no extra args needed here
- // 'post_id', // return post ids
- // 'child' // return the related child post(s) in this relationship
- // );
- // // Array Downloads
- // $downloadFiles = array();
- // foreach ($downloads as $download ) {
- // $downloadFiles[] = get_post_meta($download, 'wpcf-download-file', true);
- // }
- // // print_r($downloadFiles);
- // // Array Files
- // $files = get_post_meta( $post_id, 'wpcf-upload-subtitle' );
- // // print_r($subtitleFiles);
- // $array1 = $files;
- // $array2 = $downloadFiles;
- // // insert download
- // $resultsInsert = array_diff($array1, $array2);
- // // delete download
- // $resultsDelete = array_diff($array2, $array1);
- // // print_r($results);
- // if (!empty($resultsInsert) || empty($downloads)) {
- // foreach ($resultsInsert as $resultInsert) {
- // $single_value = $resultInsert;
- // //Replace url to directory path
- // $path = str_replace( site_url('/'), ABSPATH, esc_url( $resultInsert) );
- // $filename = basename($path);
- // $filesize = size_format( filesize( $path ) );
- // // Create post object
- // $download_post = array(
- // 'post_title' => get_the_title( $post_id ),
- // 'post_status' => 'publish',
- // 'post_type' => 'download'
- // );
- // $new_post_id = wp_insert_post( $download_post );
- // add_post_meta( $new_post_id, 'wpcf-download-file', $single_value );
- // update_post_meta( $new_post_id, 'wpcf-download-counts', '0');
- // update_post_meta( $new_post_id, 'wpcf-filename', $filename);
- // update_post_meta( $new_post_id, 'wpcf-filesize', $filesize);
- // toolset_connect_posts( 'subtitle-download', $post_id, $new_post_id );
- // }
- // }
- // if (!empty($resultsDelete)) {
- // $query = new WP_Query(
- // array(
- // 'post_type' => 'download', //Child post type slug
- // 'numberposts' => -1,
- // 'toolset_relationships' => array(
- // 'role' => 'child',
- // 'related_to' => $post_id, // ID of starting post
- // 'relationship' =>'subtitle-download',
- // ),
- // 'meta_query' => array(
- // array(
- // 'key' => 'wpcf-download-file',
- // 'value' => $resultsDelete,
- // 'compare' => 'IN',
- // ),
- // ),
- // )
- // );
- // $related_download_posts = $query->posts;
- // // Loop
- // if ( $query->have_posts() ) {
- // while ( $query->have_posts() ) {
- // $query->the_post();
- // wp_delete_post( get_the_ID(), true );
- // }
- // }
- // wp_reset_postdata();
- // }
- // }
- // }
- // // Redirect to download file
- // add_filter('cred_success_redirect', 'custom_download_redirect',10,3);
- // function custom_download_redirect($url, $post_id, $form_data){
- // if ($form_data['id']==42521) {
- // $url = get_post_meta($post_id, 'wpcf-download-file', true);
- // }
- // return $url;
- // }
- // // +1 downloads
- // add_action('cred_save_data', 'download_counter_data_action',10,2);
- // function download_counter_data_action($post_id, $form_data){
- // if ($form_data['id']==42521){
- // $counter = get_post_meta($post_id, 'wpcf-download-counts', true);
- // $counter_increment = $counter + 1;
- // update_post_meta( $post_id, 'wpcf-download-counts', $counter_increment);
- // }
- // }
- /* Cleanup files from database and server upon submitting specific subtitle cred form - edit subtitle */
- add_action('cred_save_data','images_cleanup', 10, 2);
- function images_cleanup($post_id, $form_data) {
- # if a specific form or two..
- $forms = array( 42138 );
- if (in_array($form_data['id'], $forms)){
- $type = get_post_type($post_id, $form_data);
- # if a specific post type..
- if ($type == 'subtitle-project') {
- global $wpdb;
- # Compare both tables and remove duplicates while listing only the ID from wp_post as attachment
- $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')") );
- foreach ($allposts as $singlepost) {
- $post_id_n1=$singlepost->id;
- wp_delete_attachment($post_id_n1, true);
- } # END global $wpdb;
- } # END specific post type
- } # END specific form
- } # END function images_cleanup
- // 2. Function display attachment in different ways
- add_shortcode('display-file-size', function($atts){
- global $post;
- $atts = shortcode_atts( array(
- 'file_url' =>'' ,
- 'meta_name' => 'name',
- 'meta_name' => 'size',
- 'meta_name' => 'caption',
- 'meta_name' => 'description',
- 'meta_name' => 'title',
- 'meta_name' => 'ext',
- 'meta_name' => 'count',
- ), $atts );
- $res = '';
- $attachment_id = attachment_url_to_postid($atts['file_url']);
- $file = get_attached_file($attachment_id);
- $attachment = get_post($attachment_id);
- $filetype = wp_check_filetype( $file );
- switch($atts['meta_name']){
- case 'name':
- $fileparts = explode('/', $file);
- $res = end($fileparts);
- break;
- }
- switch($atts['meta_name']){
- case 'size':
- $res = size_format(filesize( $file ));
- break;
- }
- switch($atts['meta_name']){
- case 'caption':
- $res = wp_get_attachment_caption($attachment_id);
- break;
- }
- switch($atts['meta_name']){
- case 'description':
- $res = $attachment->post_content;
- break;
- }
- switch($atts['meta_name']){
- case 'title':
- $res = get_the_title($attachment_id);
- break;
- }
- switch($atts['meta_name']){
- case 'ext':
- $res = $filetype['ext'];
- break;
- }
- switch($atts['meta_name']){
- case 'count':
- $res = get_post_meta($attachment_id, 'wpcf-download-counts', true);
- break;
- }
- return $res;
- });
- // 2 function +1 downloads and update modified date
- add_action('cred_save_data', 'download_counter_data_action', 10, 2 );
- function download_counter_data_action($post_id, $form_data){
- if ($form_data['id']==42521){
- global $wpdb;
- $date = get_the_date( 'Y-m-d H:i:s', $post_id );
- $post_modified = $date;
- $post_modified_gmt = get_gmt_from_date($date);
- $wpdb->query("UPDATE $wpdb->posts SET post_modified = '{$post_modified}', post_modified_gmt = '{$post_modified_gmt}' WHERE ID = {$post_id}" );
- $attachment_id = attachment_url_to_postid($_POST['file']);
- $counter = get_post_meta($attachment_id, 'wpcf-download-counts', true);
- $counter_increment = $counter + 1;
- update_post_meta( $attachment_id, 'wpcf-download-counts', $counter_increment);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement