Search code examples
phpwordpress

Post curl data save twice using plugin hook function wordpress


    public function __construct() {
            add_action('init', array(&$this, 'init'));
            add_action('admin_init', array(&$this, 'admin_init'));
            add_action('admin_menu', array(&$this, 'add_page'));
            add_action('admin_notices', array(&$this,'socipilot_admin_notice') );
            add_action('admin_bar_menu', array(&$this, 'socipilot_adminbar_links' ), 1001 );
            add_action('add_meta_boxes', array(&$this, 'add_meta_box' ) );
            add_action('admin_enqueue_scripts', array(&$this,'socipilot_enqueue_scripts')); 

            add_action('save_post', array(&$this, 'save'));
            //add_action('publish_post', array(&$this, 'save'));
            //add_action('edit_post', array(&$this, 'save'));
            //add_action('pre_post_update',array(&$this, 'save'));

            add_filter('plugin_action_links_'.SOCI_PILOT_PLUGIN_BASENAME, array(&$this,'ts_add_plugin_action_links'));
            // Listen for the activate event
            register_activation_hook(SOCI_PILOT_FILE, array(&$this, 'activate'));
            // Deactivation plugin
            register_deactivation_hook(SOCI_PILOT_FILE, array(&$this, 'deactivate'));
        }




    public function save($post_id) {
               global $post;


                    // Autosave, do nothing
                    if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) 
                            return;
                    // AJAX? Not used here
                    if ( defined( 'DOING_AJAX' ) && DOING_AJAX ) 
                            return;
                    // Check user permissions
                    if ('post' == $_POST['post_type']){
                        if ( ! current_user_can( 'edit_post', $post_id ) )
                        return;
                    } 
                    // Return if it's a post revision
                    if ( false !== wp_is_post_revision( $post_id ) )
                            return;


                    // Check if our nonce is set.
                if ( ! isset( $_POST['socipilot_inner_custom_box_nonce'] ) )
                return;

            $nonce = $_POST['socipilot_inner_custom_box_nonce'];

                    // Verify that the nonce is valid.
            if ( ! wp_verify_nonce( $nonce, 'socipilot_inner_custom_box' ) )
                return;

                   if(isset($_POST['soci-pilot'])):
                        $all_sociuser_posts = $_POST['soci-pilot'];
                        foreach ($all_sociuser_posts as $key=>$val){
                            $mydata[] = sanitize_text_field($key);
                        }

                        if(!empty($mydata)):


                            $mydata1 = get_post_meta( $post->ID, '_socipilot_meta_value_key', true );

                            if(!empty($mydata1)):
                                $update_soci = array_unique(array_merge($mydata1,$mydata));
                                update_post_meta( $post_id, '_socipilot_meta_value_key', $update_soci);
                            else:
                                update_post_meta( $post_id, '_socipilot_meta_value_key', $mydata);
                            endif;

                            update_post_meta( $post_id, 'soci-pilot_account_type', $_POST['soci-pilot_account_type']);
                            update_post_meta( $post_id, 'soci-pilot_schedule_date', $_POST['soci-pilot_schedule_date']);

                            $options = get_option($this->option_name);

                             $data[].= implode(',',$mydata);
                             $data[].= get_permalink($post_id);
                             $data[].= $_POST['post_title'];
                             $data[].= wp_get_attachment_url(get_post_thumbnail_id($_POST['post_ID']));
                             if($_POST['post_excerpt']):
                                $len = strpos(substr(strip_tags($_POST['post_excerpt']),25,100),'.')+1;
                                $data[].= substr(strip_tags($_POST['post_excerpt']),0,25+$len);
                             else:
                                $len = strpos(substr(strip_tags($_POST['post_content']),25,100),'.')+1;
                                $data[].= substr(strip_tags($_POST['post_content']),0,25+$len);
                            endif;

                            $data[].= $_POST['soci-pilot_account_type'];

                            if($_POST['soci-pilot_post_is_auto']=='Y'){
                                $data[].= "";
                            }else{
                                $data[].= $_POST['soci-pilot_schedule_date'];
                            }

                            if(($options['public_key']!="")&&($options['private_key']!="")):
                                $res = actionTest($options['public_key'],$options['private_key']); 
                                $error_id = $res['error'];
                            else:
                                $error_id = 1;
                            endif;

//curl function
                            if($error_id==0 && isset($res)):
                                $res = actionPosttest($options['public_key'],$options['private_key'],$data);
                                if($res->error==1){
                                     global $my_error;
                                     $my_error = new WP_Error('error', __($res->msg));
                                }
                            endif;
                        else:
                            //add_post_meta($post_id, '_socipilot_meta_value_key', $mydata, true);
                            update_post_meta( $post_id, '_socipilot_meta_value_key', $mydata);
                        endif;
                  else:      
                        $update_soci = get_post_meta( $post->ID, '_socipilot_meta_value_key', true );
                        update_post_meta( $post_id, '_socipilot_meta_value_key', $update_soci);
                  endif;

                  return;
            }

Solution

  • Using this code i have fix the issue curl function on twice data post on post_save.

    More info this code

       <?php
                   global $my_save_post_flag;
                           // .. some stuff here ..
                   if ($my_save_post_flag == 0) {
                          // .. do the stuff only once i have add my curl code hear ..
                   }  
                         // .. continue your stuff ..
                   $my_save_post_flag = 1;
            ?>
    

    my fixing code:

        <?php 
            global $socipost_save_post_flag;
    
            if($error_id==0 && isset($res)):
                if ($socipost_save_post_flag == 0){
                    $res = actionPosttest($options['public_key'],$options['private_key'],$data);
    
                    //this is my curl function so i need only one execution 
                } 
            $socipost_save_post_flag =1;
    
            endif;
        ?>