Advertisement
maxworkingwell

sort.php

Feb 3rd, 2024
1,221
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 14.27 KB | None | 0 0
  1. <?php
  2. /**
  3.  * New custom code snippet (replace this with snippet description).
  4.  */
  5.  
  6. toolset_snippet_security_check() or die( 'Direct access is not allowed' );
  7.  
  8. add_action('cred_submit_complete', 'my_submit_data_action', 10, 2);
  9. function my_submit_data_action($post_id, $form_data) {
  10.     $forms = array(3747);
  11.     if (in_array($form_data['id'], $forms)) {
  12.         error_log('submit complete');
  13.         $userid = get_current_user_id();
  14.         $posttitle = get_the_title($post_id);
  15.         $username = wp_get_current_user()->display_name;
  16.         $dated = date('d-m-Y - h:i a');
  17.         $type = "undefined";
  18.         $meta = get_post_meta($post_id);
  19.  
  20.         $sectorId = $_POST['@sector_route_parent'];
  21.  
  22.         error_log('AFTER SAVE' . $post_id . ' -> ' . $posttitle . ' Sector ID= ' . $sectorId);
  23.  
  24.         $myposition = get_post_meta($post_id, 'wpcf-route-position', true);
  25.  
  26.         $related_posts = toolset_get_related_posts(
  27.             $sectorId,
  28.             $_POST['rel-slug'],
  29.             [
  30.                 'query_by_role' => 'parent',
  31.                 'role_to_return' => 'child',
  32.                 'args' => [
  33.                     'meta_key' => 'wpcf-route-position',
  34.                 ],
  35.                 'orderby' => 'meta_value_num',
  36.                 'order' => 'ASC',
  37.                 'limit' => -1,
  38.                 'return' => 'post_id',
  39.             ]
  40.         );
  41.  
  42.         if (!empty($related_posts)) {
  43.             error_log("Related Posts Array: " . print_r($related_posts, true));
  44.  
  45.             $new_route_index = array_search($post_id, $related_posts);
  46.  
  47.             // Insert the new route at the correct position
  48.             array_splice($related_posts, $new_route_index, 1);
  49.             array_splice($related_posts, $myposition - 1, 0, $post_id);
  50.  
  51.             // Update the route positions
  52.             $i = 1;
  53.             foreach ($related_posts as $related_post) {
  54.                 $curtitle = get_the_title($related_post);
  55.               $curpos = get_post_meta($related_post, 'wpcf-route-position', true);
  56.                 $newpos = str_pad($i, 2, '0', STR_PAD_LEFT);
  57.                 update_post_meta($related_post, 'wpcf-route-position', $newpos);
  58.  
  59.                 error_log("curpos = " . $curpos . " ------ newpos = " . $newpos . " -------- " . $curtitle);
  60.  
  61.                 $i++;
  62.             }
  63.         }
  64.     }
  65. }
  66.  
  67.  
  68.  
  69.  
  70. add_action('cred_save_data', 'salveaza_campuri',10,2);
  71. function salveaza_campuri($post_id, $form_data)
  72. {  
  73.     $userid = get_current_user_id();
  74.     $posttitle = get_the_title($post_id);
  75.     $username = wp_get_current_user()->display_name;
  76.     // $datets = date();
  77.     $dated = date('d-m-Y - h:i a');
  78.     $type = "undefined";
  79.  
  80.  
  81.     // if a specific form
  82.     if ($form_data['id']==3747){
  83.         $action = "New";
  84.         $type = "route";       
  85.     }
  86.     if ($form_data['id']==3724){
  87.         $action = "New";
  88.         $type = "sector";
  89.     }
  90.     if ($form_data['id']==5470){
  91.         $action = "New";
  92.         $type = "note";
  93.         $content = $_POST['wpcf-notes'];
  94.     }
  95.     if(in_array($form_data['id'], array(3747,3724,5470))){
  96.           //Log entry    
  97.             $logentrynew = $dated. " - User '".$username. "' added a new ". $type ." '".$posttitle."'.";
  98.             if ($type == "note"){
  99.                 $logentrynew = $dated. " - User '".$username. "' added a new ". $type ." to the route '".$posttitle."': ". $content ;
  100.             }
  101.             $args = array(
  102.               'post_type' => 'log-entry',
  103.               'post_title'    => $logentrynew,
  104.               'post_status'   => 'publish',
  105.               'post_author'   => $userid,
  106.               'meta_input'   => array(
  107.                 'wpcf-entry-date' => $dated,
  108.                 'wpcf-action-related-id' => $post_id,
  109.                 'wpcf-action-related-type' => $type,
  110.                 'wpcf-user-id' => $userid,
  111.                 'wpcf-entry-user-name' => $username,
  112.                 'wpcf-action' => $action,
  113.                 'wpcf-changed' => 'all fields'
  114.                ),
  115.             );
  116.             $post_id = wp_insert_post($args);
  117.             if(!is_wp_error($post_id)){
  118.               //the post is valid
  119.             }else{
  120.               //there was an error in the post insertion,
  121.               echo $post_id->get_error_message();
  122.             }
  123.      }
  124. }
  125.  
  126.  
  127. function my_delete_function($post_id) {
  128.  
  129.         $userid = get_current_user_id();
  130.         $posttitle = get_the_title($post_id);
  131.         $username = wp_get_current_user()->display_name;
  132.         // $datets = date();
  133.         $dated = date('d-m-Y - h:i a');
  134.         $type = "undefined";
  135.  
  136.         if ( get_post_type($post_id) == 'route' ) {
  137.                     $action = "Delete";
  138.                     $type = "route";          
  139.                     $myposition = get_post_meta(  $post_id, 'wpcf-route-position', true );
  140.  
  141.     $sectorId1 = toolset_get_related_post($post_id, 'sector_route', 'parent');
  142.     $sectorId2 = toolset_get_related_post($post_id, 'settore-sicilia-route', 'parent');
  143.  
  144.     if (!empty($sectorId1)) {
  145.         $relSlug = 'sector_route';
  146.         $sectorId = $sectorId1;
  147.     } elseif (!empty($sectorId2)) {
  148.         $relSlug = 'settore-sicilia-route';
  149.         $sectorId = $sectorId2;
  150.     } else {
  151.         // exit if both $sectorId are empty
  152.         return;
  153.     }
  154.  
  155.     $related_posts = toolset_get_related_posts(
  156.         $sectorId,
  157.         $relSlug,
  158.         [
  159.             'query_by_role' => 'parent',
  160.             'role_to_return' => 'child',
  161.             'args' => [
  162.                 'meta_key' => 'wpcf-route-position',
  163.             ],
  164.             'orderby' => 'meta_value_num',
  165.             'order' => 'ASC',
  166.             'limit' => -1,
  167.             'return' => 'post_id',
  168.         ]
  169.     );
  170.          
  171.         if (!empty($related_posts)) {
  172.         $i = 1;
  173.  
  174.         foreach ($related_posts as $related_post_id) {
  175.             $curid = $related_post_id;
  176.             $curtitle = get_the_title($curid);
  177.  
  178.             // Пропускаем текущий пост
  179.             if ($post_id != $curid) {
  180.               error_log('aici');
  181.                 $curpos = get_post_meta($curid, 'wpcf-route-position', true);
  182.  
  183.                 if ($i < 10) {
  184.                     $newpos = str_pad($i, 2, '0', STR_PAD_LEFT);
  185.                 } else {
  186.                     $newpos = $i;
  187.                 }
  188.  
  189.                 update_post_meta($curid, 'wpcf-route-position', $newpos);
  190.                 $i++;
  191.  
  192.                 error_log("curpos = " . $curpos . " ------ newpos = " . $newpos . " -------- " . $curtitle);
  193.             }
  194.         }
  195.     }
  196.          
  197.                     //Log entry    
  198.                     $logentrydelete = $dated. " - User '".$username. "' deleted the ". $type ." '".$posttitle."'.";
  199.                     $args = array(
  200.                       'post_type' => 'log-entry',
  201.                       'post_title'    => $logentrydelete,
  202.                       'post_status'   => 'publish',
  203.                       'post_author'   => $userid,
  204.                       'meta_input'   => array(
  205.                         'wpcf-entry-date' => $dated,
  206.                         'wpcf-action-related-id' => $post_id,
  207.                         'wpcf-action-related-type' => $type,
  208.                         'wpcf-user-id' => $userid,
  209.                         'wpcf-entry-user-name' => $username,
  210.                         'wpcf-action' => $action,
  211.                         'wpcf-changed' => 'all fields'
  212.                       ),
  213.                     );
  214.                     $post_id = wp_insert_post($args);
  215.                     if(!is_wp_error($post_id)){
  216.                       //the post is valid
  217.                     }else{
  218.                       //there was an error in the post insertion,
  219.                       echo $post_id->get_error_message();
  220.                     }
  221.          
  222.         } else if ( get_post_type($post_id) == 'sector' ){
  223.                     $action = "Delete";
  224.                     $type = "sector";  
  225.          
  226.                     //Log entry    
  227.                     $logentrydelete = $dated. " - User '".$username. "' deleted the ". $type ." '".$posttitle."'.";
  228.                     $args = array(
  229.                       'post_type' => 'log-entry',
  230.                       'post_title'    => $logentrydelete,
  231.                       'post_status'   => 'publish',
  232.                       'post_author'   => $userid,
  233.                       'meta_input'   => array(
  234.                         'wpcf-entry-date' => $dated,
  235.                         'wpcf-action-related-id' => $post_id,
  236.                         'wpcf-action-related-type' => $type,
  237.                         'wpcf-user-id' => $userid,
  238.                         'wpcf-entry-user-name' => $username,
  239.                         'wpcf-action' => $action,
  240.                         'wpcf-changed' => 'all fields'
  241.                       ),
  242.                     );
  243.                     $post_id = wp_insert_post($args);
  244.                     if(!is_wp_error($post_id)){
  245.                       //the post is valid
  246.                     }else{
  247.                       //there was an error in the post insertion,
  248.                       echo $post_id->get_error_message();
  249.                     }
  250.         }
  251.  
  252. }
  253. add_action('wp_trash_post', 'my_delete_function');
  254.  
  255. add_action('cred_before_save_data', 'my_before_save_data_action',10,1); //edit forms
  256. function my_before_save_data_action($form_data)
  257. {
  258.     $postid = get_the_ID();
  259.     $userid = get_current_user_id();
  260.     $posttitle = get_the_title($postid);
  261.     $username = wp_get_current_user()->display_name;
  262.     // $datets = date();
  263.     $dated = date('d-m-Y - h:i a');
  264.     $type = "undefined";
  265.  
  266.     // if a specific form
  267.     if ($form_data['id']==3750)//edit routes
  268.     {
  269.         $action = "Edit";
  270.         $type = "route";
  271.      
  272.         $changedfields = array();
  273.         $fields = array('wpcf-route-position','wpcf-grade','wpcf-height','wpcf-bolter-s','wpcf-year-bolted','wpcf-stars','wpcf-climbing-type','wpcf-pitches-grades','wpcf-n-pitches','wpcf-pitches-length','wpcf-oblig-grade','wpcf-equipment-used','wpcf-year-re-bolted','wpcf-notes','wpcf-danger','wpcf-id-unico','wpcf-path','wpcf-route-bolting-type');
  274.         $fnames = array('Position','Route grade','Height','Who bolted','Year bolted','Stars','Climbing type','Pitches grades','No. of pitches','Pitches length','Oblig. grade','Equipment used','Year re-bolted','Notes','Danger','ID','Path','Bolting type');
  275.         $newvalues = array();
  276.      
  277.       foreach ($fields as $field) {
  278.         if (isset($_POST[$field]))
  279.           {
  280.               $oldvalue = get_post_meta($postid, $field, true);
  281.               $newvalue = $_POST[$field];
  282.          
  283.               if ($oldvalue != $newvalue){
  284.                 $changedfields[]=$field;
  285.                 $newvalues[]=$newvalue;
  286.               }
  287.           }        
  288.        }
  289.       $oldtitle = get_the_title($postid);
  290.       $newtitle = $_POST['post_title'];
  291.       if ($oldtitle != $newtitle){
  292.         $changedfields[]='Title';
  293.         $newvalues[]=$newtitle;
  294.       }
  295.       if (empty($changedfields)){
  296.         $changedfields[]='none';
  297.         $newvalues[]='none';
  298.       }
  299.      
  300.       $changedpairs = array_combine($changedfields,$newvalues);
  301.      
  302.       foreach ($changedpairs as $changedfield => $changedvalue){
  303.         $a[]=$changedfield.': '.$changedvalue;
  304.       }
  305.       $chfields = implode(', ',$a);
  306.       $chfields = str_replace($fields, $fnames, $chfields);
  307.       //Log entry    
  308.       $logentryedit = $dated. " - User '".$username. "' changed the ". $type ." '".$posttitle."'. Fields changed: ".$chfields ;
  309.       $args = array(
  310.         'post_type' => 'log-entry',
  311.         'post_title'    => $logentryedit,
  312.         'post_status'   => 'publish',
  313.         'post_author'   => $userid,
  314.         'meta_input'   => array(
  315.           'wpcf-entry-date' => $dated,
  316.           'wpcf-action-related-id' => $postid,
  317.           'wpcf-action-related-type' => $type,
  318.           'wpcf-user-id' => $userid,
  319.           'wpcf-entry-user-name' => $username,
  320.           'wpcf-action' => $action,
  321.           'wpcf-changed' => $chfields
  322.         ),
  323.       );
  324.       $post_id = wp_insert_post($args);
  325.       if(!is_wp_error($post_id)){
  326.         //the post is valid
  327.       }else{
  328.         //there was an error in the post insertion,
  329.         echo $post_id->get_error_message();
  330.       }
  331.  
  332.     }
  333.  
  334.  
  335.     if ($form_data['id']==3722)//edit sectors
  336.     {
  337.      
  338.         $action = "Edit";
  339.         $type = "sector";
  340.      
  341.         $changedfields = array();
  342.         $fields = array('wpcf-sector-thumb','wpcf-sector-location','wpcf-sector-description','wpcf-warnings','wpcf-id','wpcf-approach','wpcf-sun','wpcf-bolting-type','wpcf-sheltered-wind','wpcf-inclination','wpcf-best-season','wpcf-rock-type','wpcf-svg-soste','wpcf-risky-sector');
  343.         $fnames = array('Thumb image','Location','Sector description','Warnings','Id','Approach','Sun','Bolting type','Sheltered wind','Inclination','Best season','Rock type','SVG soste','Risky sector');
  344.         $newvalues = array();
  345.      
  346.       foreach ($fields as $field) {
  347.         if (isset($_POST[$field]))
  348.           {
  349.               $oldvalue = get_post_meta($postid, $field, true);
  350.               $newvalue = $_POST[$field];
  351.          
  352.               if ($oldvalue != $newvalue){
  353.                 $changedfields[]=$field;
  354.                 $newvalues[]=$newvalue;
  355.               }
  356.           }        
  357.        }
  358.       $oldtitle = get_the_title($postid);
  359.       $newtitle = $_POST['post_title'];
  360.       if ($oldtitle != $newtitle){
  361.         $changedfields[]='Title';
  362.         $newvalues[]=$newtitle;
  363.       }
  364.       if (empty($changedfields)){
  365.         $changedfields[]='none';
  366.         $newvalues[]='none';
  367.       }
  368.       $changedpairs = array_combine($changedfields,$newvalues);
  369.      
  370.       foreach ($changedpairs as $changedfield => $changedvalue){
  371.         $a[]=$changedfield.': '.$changedvalue;
  372.       }
  373.       $chfields = implode(', ',$a);
  374.       $chfields = str_replace($fields, $fnames, $chfields);
  375.       //Log entry    
  376.       $logentryedit = $dated. " - User '".$username. "' changed the ". $type ." '".$posttitle."'.  Fields changed: ".$chfields ;
  377.       $args = array(
  378.         'post_type' => 'log-entry',
  379.         'post_title'    => $logentryedit,
  380.         'post_status'   => 'publish',
  381.         'post_author'   => $userid,
  382.         'meta_input'   => array(
  383.           'wpcf-entry-date' => $dated,
  384.           'wpcf-action-related-id' => $postid,
  385.           'wpcf-action-related-type' => $type,
  386.           'wpcf-user-id' => $userid,
  387.           'wpcf-entry-user-name' => $username,
  388.           'wpcf-action' => $action,
  389.           'wpcf-changed' => $chfields
  390.         ),
  391.       );
  392.       $post_id = wp_insert_post($args);
  393.       if(!is_wp_error($post_id)){
  394.         //the post is valid
  395.       }else{
  396.         //there was an error in the post insertion,
  397.         echo $post_id->get_error_message();
  398.       }
  399.     }  
  400.  
  401.  
  402. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement