我试图使用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;
}
}
我再次注意到,当我将类代码直接放在函数中时,它是有效的