%PDF- %PDF-
Mini Shell

Mini Shell

Direktori : /var/www/pn/utils/classes/
Upload File :
Create Path :
Current File : /var/www/pn/utils/classes/ActionProcessor.php

<?php 
class ActionProcessor
{
    public $wpdb;
    public $data;

    public function __construct($data)
    {
        global $wpdb;
        $this->wpdb = $wpdb;
        $this->data = $data;
    }

    public function prepareData($data)
    {
        unset($data['id']);
        unset($data['action']);

        return $data;
    }

    public function save()
    {   
        $dataToSave = $this->prepareData($this->data);
        $result = $this->wpdb->update( PROJECT_TABLE_NAME,
            $dataToSave,
            array( 'id' => $this->data['id'] )
        );    

        return $result;
    }    

    public function approve($id=null)
    {
        $id = !empty($id) ? $id : $this->data['id'];
        $data = array('approved' => true);
        $result = $this->wpdb->update( PROJECT_TABLE_NAME,
            $data,
            array( 'id' => $id )
        ); 
        
        return $result;
    }

    public function getProjectDataById($id)
    {
        $sql = "SELECT * FROM " . PROJECT_TABLE_NAME . " WHERE id=".$id;
        $projectData = $this->wpdb->get_row( $sql, OBJECT );

        return $projectData;
    }

    public function createProjectInWpIfNotYet($id=null)
    {
        $id = !empty($id) ? $id : $this->data['id'];
        $projectData = $this->getProjectDataById($id);

        if($projectData->post_id){
            return false;
        }
        $postData = array(
            'post_title'    => wp_strip_all_tags( $projectData->name ),
            'post_content'  => $projectData->description,
            'post_type'     => 'project',
            'post_status'   => 'draft',
            'post_author'   => 1,
        );            
        $postId = wp_insert_post( $postData );

        if($postId){
            $data = array('post_id' => $postId);
            $this->wpdb->update( PROJECT_TABLE_NAME,
                $data,
                array( 'id' => $id )
            );
        }

        $this->addCountryToWpProject($postId, $projectData->beneficiarySAI);

        //ACF fields update
        update_field('field_59e86f0e28f89', $projectData->status, $postId);//project_status //Status of project*
        update_field('field_59e872cdc6c3d', $projectData->implementingAgency, $postId);//imp_agencys //Implementing agency(s)       
        update_field('field_59e86fd028f8a', $projectData->beneficiaryLevel, $postId); //beneficiary_level field_59e86fd028f8a
        update_field('field_59e8704428f8b', $projectData->intosaiRegion, $postId);//intosai_region field_59e8704428f8b

        return $postId;
    }

    public function addCountryToWpProject($postId, $countrys)
    {
        $countrys = explode(',', $countrys);
        foreach($countrys as $country){
            $country = trim($country);
            $term = term_exists( $country, 'wdi');
            if(!empty($term['term_id'])){
                wp_set_post_terms( $postId, array($term['term_id']), 'wdi', true );
            }  
        }
      
    }

    public function unapprove($id=null)
    {
        $id = !empty($id) ? $id : $this->data['id'];
        $data = array('approved' => false);
        
        $result = $this->wpdb->update( PROJECT_TABLE_NAME,
            $data,
            array( 'id' => $id )
        ); 

        return $result;
    }

    public function delete($id=null)
    {
        $id = !empty($id) ? $id : $this->data['id'];
        $data = array('deleted' => true);
        
        $result = $this->wpdb->update( PROJECT_TABLE_NAME,
            $data,
            array( 'id' => $id )
        ); 

        return $result;
    }

    public function saveKeys()
    {
        try{
            file_put_contents(PARSER_PATH.'/keywords.txt', $this->data['keywords']);
        }catch (\Exception $e) {
                echo 'Can`t save keywords: ',  $e->getMessage(), "\n";
                echo 'In file: ',  PARSER_PATH.'/keywords.txt', "\n";
        }        
    }
}

Zerion Mini Shell 1.0