Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /**
- * New custom code snippet (replace this with snippet description).
- */
- toolset_snippet_security_check() or die( 'Direct access is not allowed' );
- add_action('cred_submit_complete', 'my_submit_data_action', 10, 2);
- function my_submit_data_action($post_id, $form_data) {
- $forms = array(3747);
- if (in_array($form_data['id'], $forms)) {
- error_log('submit complete');
- $userid = get_current_user_id();
- $posttitle = get_the_title($post_id);
- $username = wp_get_current_user()->display_name;
- $dated = date('d-m-Y - h:i a');
- $type = "undefined";
- $meta = get_post_meta($post_id);
- $sectorId = $_POST['@sector_route_parent'];
- error_log('AFTER SAVE' . $post_id . ' -> ' . $posttitle . ' Sector ID= ' . $sectorId);
- $myposition = get_post_meta($post_id, 'wpcf-route-position', true);
- $related_posts = toolset_get_related_posts(
- $sectorId,
- $_POST['rel-slug'],
- [
- 'query_by_role' => 'parent',
- 'role_to_return' => 'child',
- 'args' => [
- 'meta_key' => 'wpcf-route-position',
- ],
- 'orderby' => 'meta_value_num',
- 'order' => 'ASC',
- 'limit' => -1,
- 'return' => 'post_id',
- ]
- );
- if (!empty($related_posts)) {
- error_log("Related Posts Array: " . print_r($related_posts, true));
- $new_route_index = array_search($post_id, $related_posts);
- // Insert the new route at the correct position
- array_splice($related_posts, $new_route_index, 1);
- array_splice($related_posts, $myposition - 1, 0, $post_id);
- // Update the route positions
- $i = 1;
- foreach ($related_posts as $related_post) {
- $curtitle = get_the_title($related_post);
- $curpos = get_post_meta($related_post, 'wpcf-route-position', true);
- $newpos = str_pad($i, 2, '0', STR_PAD_LEFT);
- update_post_meta($related_post, 'wpcf-route-position', $newpos);
- error_log("curpos = " . $curpos . " ------ newpos = " . $newpos . " -------- " . $curtitle);
- $i++;
- }
- }
- }
- }
- add_action('cred_save_data', 'salveaza_campuri',10,2);
- function salveaza_campuri($post_id, $form_data)
- {
- $userid = get_current_user_id();
- $posttitle = get_the_title($post_id);
- $username = wp_get_current_user()->display_name;
- // $datets = date();
- $dated = date('d-m-Y - h:i a');
- $type = "undefined";
- // if a specific form
- if ($form_data['id']==3747){
- $action = "New";
- $type = "route";
- }
- if ($form_data['id']==3724){
- $action = "New";
- $type = "sector";
- }
- if ($form_data['id']==5470){
- $action = "New";
- $type = "note";
- $content = $_POST['wpcf-notes'];
- }
- if(in_array($form_data['id'], array(3747,3724,5470))){
- //Log entry
- $logentrynew = $dated. " - User '".$username. "' added a new ". $type ." '".$posttitle."'.";
- if ($type == "note"){
- $logentrynew = $dated. " - User '".$username. "' added a new ". $type ." to the route '".$posttitle."': ". $content ;
- }
- $args = array(
- 'post_type' => 'log-entry',
- 'post_title' => $logentrynew,
- 'post_status' => 'publish',
- 'post_author' => $userid,
- 'meta_input' => array(
- 'wpcf-entry-date' => $dated,
- 'wpcf-action-related-id' => $post_id,
- 'wpcf-action-related-type' => $type,
- 'wpcf-user-id' => $userid,
- 'wpcf-entry-user-name' => $username,
- 'wpcf-action' => $action,
- 'wpcf-changed' => 'all fields'
- ),
- );
- $post_id = wp_insert_post($args);
- if(!is_wp_error($post_id)){
- //the post is valid
- }else{
- //there was an error in the post insertion,
- echo $post_id->get_error_message();
- }
- }
- }
- function my_delete_function($post_id) {
- $userid = get_current_user_id();
- $posttitle = get_the_title($post_id);
- $username = wp_get_current_user()->display_name;
- // $datets = date();
- $dated = date('d-m-Y - h:i a');
- $type = "undefined";
- if ( get_post_type($post_id) == 'route' ) {
- $action = "Delete";
- $type = "route";
- $myposition = get_post_meta( $post_id, 'wpcf-route-position', true );
- $sectorId1 = toolset_get_related_post($post_id, 'sector_route', 'parent');
- $sectorId2 = toolset_get_related_post($post_id, 'settore-sicilia-route', 'parent');
- if (!empty($sectorId1)) {
- $relSlug = 'sector_route';
- $sectorId = $sectorId1;
- } elseif (!empty($sectorId2)) {
- $relSlug = 'settore-sicilia-route';
- $sectorId = $sectorId2;
- } else {
- // exit if both $sectorId are empty
- return;
- }
- $related_posts = toolset_get_related_posts(
- $sectorId,
- $relSlug,
- [
- 'query_by_role' => 'parent',
- 'role_to_return' => 'child',
- 'args' => [
- 'meta_key' => 'wpcf-route-position',
- ],
- 'orderby' => 'meta_value_num',
- 'order' => 'ASC',
- 'limit' => -1,
- 'return' => 'post_id',
- ]
- );
- if (!empty($related_posts)) {
- $i = 1;
- foreach ($related_posts as $related_post_id) {
- $curid = $related_post_id;
- $curtitle = get_the_title($curid);
- // Пропускаем текущий пост
- if ($post_id != $curid) {
- error_log('aici');
- $curpos = get_post_meta($curid, 'wpcf-route-position', true);
- if ($i < 10) {
- $newpos = str_pad($i, 2, '0', STR_PAD_LEFT);
- } else {
- $newpos = $i;
- }
- update_post_meta($curid, 'wpcf-route-position', $newpos);
- $i++;
- error_log("curpos = " . $curpos . " ------ newpos = " . $newpos . " -------- " . $curtitle);
- }
- }
- }
- //Log entry
- $logentrydelete = $dated. " - User '".$username. "' deleted the ". $type ." '".$posttitle."'.";
- $args = array(
- 'post_type' => 'log-entry',
- 'post_title' => $logentrydelete,
- 'post_status' => 'publish',
- 'post_author' => $userid,
- 'meta_input' => array(
- 'wpcf-entry-date' => $dated,
- 'wpcf-action-related-id' => $post_id,
- 'wpcf-action-related-type' => $type,
- 'wpcf-user-id' => $userid,
- 'wpcf-entry-user-name' => $username,
- 'wpcf-action' => $action,
- 'wpcf-changed' => 'all fields'
- ),
- );
- $post_id = wp_insert_post($args);
- if(!is_wp_error($post_id)){
- //the post is valid
- }else{
- //there was an error in the post insertion,
- echo $post_id->get_error_message();
- }
- } else if ( get_post_type($post_id) == 'sector' ){
- $action = "Delete";
- $type = "sector";
- //Log entry
- $logentrydelete = $dated. " - User '".$username. "' deleted the ". $type ." '".$posttitle."'.";
- $args = array(
- 'post_type' => 'log-entry',
- 'post_title' => $logentrydelete,
- 'post_status' => 'publish',
- 'post_author' => $userid,
- 'meta_input' => array(
- 'wpcf-entry-date' => $dated,
- 'wpcf-action-related-id' => $post_id,
- 'wpcf-action-related-type' => $type,
- 'wpcf-user-id' => $userid,
- 'wpcf-entry-user-name' => $username,
- 'wpcf-action' => $action,
- 'wpcf-changed' => 'all fields'
- ),
- );
- $post_id = wp_insert_post($args);
- if(!is_wp_error($post_id)){
- //the post is valid
- }else{
- //there was an error in the post insertion,
- echo $post_id->get_error_message();
- }
- }
- }
- add_action('wp_trash_post', 'my_delete_function');
- add_action('cred_before_save_data', 'my_before_save_data_action',10,1); //edit forms
- function my_before_save_data_action($form_data)
- {
- $postid = get_the_ID();
- $userid = get_current_user_id();
- $posttitle = get_the_title($postid);
- $username = wp_get_current_user()->display_name;
- // $datets = date();
- $dated = date('d-m-Y - h:i a');
- $type = "undefined";
- // if a specific form
- if ($form_data['id']==3750)//edit routes
- {
- $action = "Edit";
- $type = "route";
- $changedfields = array();
- $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');
- $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');
- $newvalues = array();
- foreach ($fields as $field) {
- if (isset($_POST[$field]))
- {
- $oldvalue = get_post_meta($postid, $field, true);
- $newvalue = $_POST[$field];
- if ($oldvalue != $newvalue){
- $changedfields[]=$field;
- $newvalues[]=$newvalue;
- }
- }
- }
- $oldtitle = get_the_title($postid);
- $newtitle = $_POST['post_title'];
- if ($oldtitle != $newtitle){
- $changedfields[]='Title';
- $newvalues[]=$newtitle;
- }
- if (empty($changedfields)){
- $changedfields[]='none';
- $newvalues[]='none';
- }
- $changedpairs = array_combine($changedfields,$newvalues);
- foreach ($changedpairs as $changedfield => $changedvalue){
- $a[]=$changedfield.': '.$changedvalue;
- }
- $chfields = implode(', ',$a);
- $chfields = str_replace($fields, $fnames, $chfields);
- //Log entry
- $logentryedit = $dated. " - User '".$username. "' changed the ". $type ." '".$posttitle."'. Fields changed: ".$chfields ;
- $args = array(
- 'post_type' => 'log-entry',
- 'post_title' => $logentryedit,
- 'post_status' => 'publish',
- 'post_author' => $userid,
- 'meta_input' => array(
- 'wpcf-entry-date' => $dated,
- 'wpcf-action-related-id' => $postid,
- 'wpcf-action-related-type' => $type,
- 'wpcf-user-id' => $userid,
- 'wpcf-entry-user-name' => $username,
- 'wpcf-action' => $action,
- 'wpcf-changed' => $chfields
- ),
- );
- $post_id = wp_insert_post($args);
- if(!is_wp_error($post_id)){
- //the post is valid
- }else{
- //there was an error in the post insertion,
- echo $post_id->get_error_message();
- }
- }
- if ($form_data['id']==3722)//edit sectors
- {
- $action = "Edit";
- $type = "sector";
- $changedfields = array();
- $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');
- $fnames = array('Thumb image','Location','Sector description','Warnings','Id','Approach','Sun','Bolting type','Sheltered wind','Inclination','Best season','Rock type','SVG soste','Risky sector');
- $newvalues = array();
- foreach ($fields as $field) {
- if (isset($_POST[$field]))
- {
- $oldvalue = get_post_meta($postid, $field, true);
- $newvalue = $_POST[$field];
- if ($oldvalue != $newvalue){
- $changedfields[]=$field;
- $newvalues[]=$newvalue;
- }
- }
- }
- $oldtitle = get_the_title($postid);
- $newtitle = $_POST['post_title'];
- if ($oldtitle != $newtitle){
- $changedfields[]='Title';
- $newvalues[]=$newtitle;
- }
- if (empty($changedfields)){
- $changedfields[]='none';
- $newvalues[]='none';
- }
- $changedpairs = array_combine($changedfields,$newvalues);
- foreach ($changedpairs as $changedfield => $changedvalue){
- $a[]=$changedfield.': '.$changedvalue;
- }
- $chfields = implode(', ',$a);
- $chfields = str_replace($fields, $fnames, $chfields);
- //Log entry
- $logentryedit = $dated. " - User '".$username. "' changed the ". $type ." '".$posttitle."'. Fields changed: ".$chfields ;
- $args = array(
- 'post_type' => 'log-entry',
- 'post_title' => $logentryedit,
- 'post_status' => 'publish',
- 'post_author' => $userid,
- 'meta_input' => array(
- 'wpcf-entry-date' => $dated,
- 'wpcf-action-related-id' => $postid,
- 'wpcf-action-related-type' => $type,
- 'wpcf-user-id' => $userid,
- 'wpcf-entry-user-name' => $username,
- 'wpcf-action' => $action,
- 'wpcf-changed' => $chfields
- ),
- );
- $post_id = wp_insert_post($args);
- if(!is_wp_error($post_id)){
- //the post is valid
- }else{
- //there was an error in the post insertion,
- echo $post_id->get_error_message();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement