将类文件与wp_ajax挂钩一起使用

时间:2019-08-20 作者:sbc

我试图使用wp\\u ajax hook中另一个文件中的类,调用返回500状态码

我用incloud()添加了类,

我试着把整个班级都放在函数中,结果很好。

有没有办法在wp\\u ajax函数中使用类文件?

这是函数中的代码。php文件:

add_action( \'init\', \'ajax_import_rows_init\' );
function ajax_import_rows_init() {

    wp_register_script( \'ajax_import_rows\', get_stylesheet_directory_uri().\'/js/ajax_import_rows.js\', array(\'jquery\') );
    wp_enqueue_script( \'ajax_import_rows\' );

    $rows_nonce = wp_create_nonce( \'ajax_rows_nonce\' );

    wp_localize_script( \'ajax_import_rows\', \'ajax_import_opject\', array(
        \'ajaxurl\'   =>  admin_url( \'admin-ajax.php\' ),
        \'nonce\'     =>  $rows_nonce
    ));

   add_action(\'wp_ajax_import_list_rows\', \'response_list_rows\');
   add_action(\'wp_ajax_nopriv_import_list_rows\', \'response_list_rows\');
}

function response_list_rows(){

    check_ajax_referer( \'ajax_rows_nonce\', \'nonce\' );

    include_once(get_stylesheet_directory_uri() . \'/fileds.php\');   

    $post_id = $_POST[\'post_id\'];

    $list_rows = new fields_ACF($post_id);
    $list_rows_json = json_encode( $list_rows -> fileds);
    echo( $list_rows_json);

    die();
}
这是字段中的类。php文件:

class fields_ACF{

public $fileds =array();

public function fields_ACF($post_id){

    $income     =     $this ->  income($post_id);
    $expenses   =     $this ->  expenses($post_id);
    $debts      =     $this ->  debts($post_id);

    return(
        $this -> fileds["income"]   =   $income and 
        $this -> fileds["expenses"] =   $expenses and 
        $this -> fileds["debts"]    =   $debts
    );
    //$this -> fileds = array();

}

//פונקציה להכנסות
private function income($post_id){

    $a =array();
    $fileds = array();

    if( have_rows(\'revenue\', $post_id) ):
        while(have_rows(\'revenue\', $post_id) ): the_row();

            /*****הוצאת כמות שיש מכל קטגוריה***********/
            array_push($a, get_sub_field(\'id_cat\', $post_id));
            $count_rows_registered = array_count_values($a);

    endwhile;
    endif;

    if( have_rows(\'types_income\', \'option\') ):
        while(have_rows(\'types_income\', \'option\') ): the_row();
            $type_no = get_sub_field(\'type_income_no\');
            $title = get_sub_field(\'type_income_name\');
            if( have_rows(\'cat_income\', \'option\') ):
                while( have_rows(\'cat_income\', \'option\') ): the_row();
                    $cat_income_no = get_sub_field(\'cat_income_no\');
                    $cat_income_name = get_sub_field(\'cat_income_name\');
                    if( have_rows(\'settings_cat\') ):
                    while( have_rows(\'settings_cat\') ): the_row();
                    $day_field  = get_sub_field(\'day_in_months\');
                    $month_field = get_sub_field(\'months\');
                    endwhile;
                    endif;                   
                    $fileds[$type_no][\'name\']=$title;
                    $fileds[$type_no][\'content\'][$cat_income_no][\'name\']= $cat_income_name;
                    $fileds[$type_no][\'content\'][$cat_income_no][\'settings\']=[
                        \'day_field\' => $day_field,
                        \'month_field\' => $month_field
                        ];
                    $fileds[$type_no][\'content\'][$cat_income_no][\'count_rows_registered\']=$count_rows_registered[$cat_income_no];
                    $fileds[$type_no][\'content\'][$cat_income_no][\'rows_registered\']=[];
                endwhile;
            endif;
        endwhile;
    endif;


    if( have_rows(\'revenue\', $post_id) ):
        while(have_rows(\'revenue\', $post_id) ): the_row();
            /********הכנסת תוכן השורות במערך הכללי********/
            $row =array(
                \'index_row\'         =>  get_row_index(),
                \'date_registered\'   =>  get_sub_field(\'Date_typed\'),
                \'type\'              =>  get_sub_field(\'type\'),
                \'ID_type\'           =>  get_sub_field(\'id_type\'),
                \'category_name\'     =>  get_sub_field(\'category_Revenue\'),
                \'ID_category\'       =>  get_sub_field(\'id_cat\'),
                \'dete_in_payment\'   =>  get_sub_field(\'Date_payment\'),
                \'Amount\'            =>  get_sub_field(\'total\'),
                \'method_payment\'    =>  get_sub_field(\'method\')
            );

            $fileds[$row[\'ID_type\']][\'content\'][$row[\'ID_category\']][\'rows_registered\'][]=$row;
        endwhile;
    endif;

    return($fileds);

}

//פונקציה להוצאות    
private function expenses($post_id){

    $a =array();
    $fileds = array();

    if( have_rows(\'expenses\', $post_id) ):
        while(have_rows(\'expenses\', $post_id) ): the_row();

            /*****הוצאת כמות שיש מכל קטגוריה***********/
            array_push($a, get_sub_field(\'id_cat\', $post_id));
            $count_rows_registered = array_count_values($a);

    endwhile;
    endif;

    if( have_rows(\'types_expenses\', \'option\') ):
        while(have_rows(\'types_expenses\', \'option\') ): the_row();
            $type_no = get_sub_field(\'type_expenses_no\');
            $title = get_sub_field(\'type_expenses_name\');
            if( have_rows(\'cat_expenses\', \'option\') ):
                while( have_rows(\'cat_expenses\', \'option\') ): the_row();
                    $cat_income_no = get_sub_field(\'cat_expenses_no\');
                    $cat_income_name = get_sub_field(\'cat_expenses_name\');
                    if( have_rows(\'settings_cat\') ):
                    while( have_rows(\'settings_cat\') ): the_row();
                    $day_field  = get_sub_field(\'day_in_months\');
                    $month_field = get_sub_field(\'months\');
                    endwhile;
                    endif;                   
                    $fileds[$type_no][\'name\']=$title;
                    $fileds[$type_no][\'content\'][$cat_income_no][\'name\']= $cat_income_name;
                    $fileds[$type_no][\'content\'][$cat_income_no][\'settings\']=[
                        \'day_field\' => $day_field,
                        \'month_field\' => $month_field
                        ];
                    $fileds[$type_no][\'content\'][$cat_income_no][\'count_rows_registered\']=$count_rows_registered[$cat_income_no];
                    $fileds[$type_no][\'content\'][$cat_income_no][\'rows_registered\']=[];

                endwhile;
            endif;
        endwhile;
    endif;


    if( have_rows(\'expenses\', $post_id) ):
        while(have_rows(\'expenses\', $post_id) ): the_row();
            /********הכנסת תוכן השורות במערך הכללי********/
            $row =array(
                \'index_row\'         =>  get_row_index(),
                \'date_registered\'   =>  get_sub_field(\'Date_typed\'),
                \'type\'              =>  get_sub_field(\'type\'),
                \'ID_type\'           =>  get_sub_field(\'id_type\'),
                \'category_name\'     =>  get_sub_field(\'category_expenses\'),
                \'ID_category\'       =>  get_sub_field(\'id_cat\'),
                \'dete_in_payment\'   =>  get_sub_field(\'Date_payment\'),
                \'Amount\'            =>  get_sub_field(\'total\'),
                \'method_payment\'    =>  get_sub_field(\'method\')
            );
            $fileds[$row[\'ID_type\']][\'content\'][$row[\'ID_category\']][\'rows_registered\'][]=$row;
        endwhile;
    endif;

    return $fileds;

}

//פונקציה לחובות    
private function debts($post_id){

    $a =array();
    $fileds = array();

    if( have_rows(\'debts\', $post_id) ):
        while(have_rows(\'debts\', $post_id) ): the_row();

            /*****הוצאת כמות שיש מכל קטגוריה***********/
            array_push($a, get_sub_field(\'id_cat\', $post_id));
            $count_rows_registered = array_count_values($a);

    endwhile;
    endif;

    if( have_rows(\'types_income\', \'option\') ):
        while(have_rows(\'types_income\', \'option\') ): the_row();
            $type_no = get_sub_field(\'type_income_no\');
            $title = get_sub_field(\'type_income_name\');
            if( have_rows(\'cat_income\', \'option\') ):
                while( have_rows(\'cat_income\', \'option\') ): the_row();
                    $cat_income_no = get_sub_field(\'cat_income_no\');
                    $cat_income_name = get_sub_field(\'cat_income_name\');
                    if( have_rows(\'settings_cat\') ):
                    while( have_rows(\'settings_cat\') ): the_row();
                    $day_field  = get_sub_field(\'day_in_months\');
                    $month_field = get_sub_field(\'months\');
                    endwhile;
                    endif;                   
                    $fileds[$type_no][\'name\']=$title;
                    $fileds[$type_no][\'content\'][$cat_income_no][\'name\']= $cat_income_name;
                    $fileds[$type_no][\'content\'][$cat_income_no][\'settings\']=[
                        \'day_field\' => $day_field,
                        \'month_field\' => $month_field
                        ];
                    $fileds[$type_no][\'content\'][$cat_income_no][\'count_rows_registered\']=$count_rows_registered[$cat_income_no];
                    $fileds[$type_no][\'content\'][$cat_income_no][\'rows_registered\']=[];

                endwhile;
            endif;
        endwhile;
    endif;


    if( have_rows(\'debts\', $post_id) ):
        while(have_rows(\'debts\', $post_id) ): the_row();
            /********הכנסת תוכן השורות במערך הכללי********/
            $row =array(
                \'index_row\'         =>  get_row_index(),
                \'date_registered\'   =>  get_sub_field(\'Date_typed\'),
                \'type\'              =>  get_sub_field(\'type\'),
                \'ID_type\'           =>  get_sub_field(\'id_type\'),
                \'category_name\'     =>  get_sub_field(\'category_debts\'),
                \'ID_category\'       =>  get_sub_field(\'id_cat\'),
                \'dete_in_payment\'   =>  get_sub_field(\'total_debt\'),
                \'Amount\'            =>  get_sub_field(\'total\'),
                \'method_payment\'    =>  get_sub_field(\'method\')
            );
            $fileds[$row[\'ID_type\']][\'content\'][$row[\'ID_category\']][\'rows_registered\'][]=$row;
        endwhile;
    endif;

    return $fileds;

}

}
我再次注意到,当我将类代码直接放在函数中时,它是有效的

2 个回复
SO网友:dragos.nicolae

仅当文件为yourclass时,才使用include(yourclass.php)。php位于同一文件夹中。否则,您需要将完整路径放入文件,或为插件路径使用一个变量:public$plugin\\u path$此->plugin\\u path=plugin\\u dir\\u path(dirname(dirname(FILE ) ) );

并使用plugin\\u path包含该文件。

SO网友:Jacob Peattie

首先,文件名拼写错误:

include_once(get_stylesheet_directory_uri() . \'/fileds.php\'); 
“fileds.php”应该是“fields.php”,这取决于您在问题的其他地方对它的称呼。

其次,在许多(如果不是大多数)服务器上,您不能将URL用于include_once. 您需要获取文件的路径。使用get_theme_file_path() 为此,请执行以下操作:

include_once get_theme_file_path( \'fields.php\' );