name = 'announce_setting'; $this->do_screen_slug = $Afd->main_slug; $this->menu_title = $Afd->name; $this->page_title = $Afd->name; $this->MainModel = new Afd_Model_Announces(); parent::__construct(); } public function admin_menu() { global $Afd; if( $Afd->Site->is_multisite ) { add_menu_page( $this->page_title , $this->menu_title , $Afd->Plugin->capability , $this->do_screen_slug , array( $this , 'view' ) ); } else { add_options_page( $this->page_title , $this->menu_title , $Afd->Plugin->capability , $this->do_screen_slug , array( $this , 'view' ) ); } } public function view() { global $Afd; include_once( $this->view_dir . 'manager-announce-setting.php' ); } private function get_data() { global $Afd; $settings_data = $this->MainModel->get_datas(); return $settings_data; } protected function post_data() { global $Afd; if( !empty( $_POST[$Afd->Form->nonce . 'add_' . $this->name] ) ) { $nonce_key = $Afd->Form->nonce . 'add_' . $this->name; if( check_admin_referer( $nonce_key , $nonce_key ) ) { $errors = $this->MainModel->add_data( $_POST ); $notice = 'update_' . $this->name; } } elseif( !empty( $_POST[$Afd->Form->nonce . 'update_' . $this->name] ) ) { $nonce_key = $Afd->Form->nonce . 'update_' . $this->name; if( check_admin_referer( $nonce_key , $nonce_key ) ) { $errors = $this->MainModel->update_data( $_POST ); $notice = 'update_' . $this->name; } } elseif( !empty( $_POST[$Afd->Form->nonce . 'remove_' . $this->name] ) ) { $nonce_key = $Afd->Form->nonce . 'remove_' . $this->name; if( check_admin_referer( $nonce_key , $nonce_key ) ) { $errors = $this->MainModel->remove_datas( $_POST ); $notice = 'remove_' . $this->name; } } if( !isset( $errors ) ) return false; $error_codes = $errors->get_error_codes(); if( !empty( $error_codes ) ) { $this->errors = $errors; } else { wp_redirect( esc_url_raw( add_query_arg( array( $Afd->Plugin->msg_notice => $notice , 'page' => $this->do_screen_slug ) , $Afd->Helper->get_action_link() ) ) ); exit; } } private function print_form_fields( $field_type = false , $edit_type = 'add' , $announce_id = false , $announce = array() ) { global $Afd; $announce_types = $Afd->Helper->get_announce_types(); $date_periods = $Afd->Helper->get_date_periods(); $multisite_show_standard = $Afd->Helper->get_multisite_show_standard(); $all_user_roles = $Afd->Helper->get_all_user_roles(); if( $Afd->Site->is_multisite ) { $all_sites = $Afd->Helper->get_sites(); } $field_base_name = 'data[' . $edit_type . ']'; $field_base_id = $edit_type; if( $edit_type == 'add' ) { $announce = $this->MainModel->get_default_data(); } else { $field_base_name .= '[' . $announce_id . ']'; $field_base_id .= '_' . $announce_id; } if( $field_type == 'title' ) { printf( '' , $field_base_name , $field_base_id , $announce['title'] ); } elseif( $field_type == 'content' ) { echo wp_editor( $announce['content'] , $field_base_id . '_content' , array( 'textarea_name' => $field_base_name . '[content]' , 'media_buttons' => false ) ); } elseif( $field_type == 'type' ) { printf( ''; } elseif( $field_type == 'date_range' ) { printf( '

%s

', sprintf( __( 'Please %1$s is later than the %2$s.' , $Afd->ltd ) , __( 'End Date' ) , __( 'Start Date' ) ) ); foreach( $date_periods as $period => $period_label ) { $add_class = $period; if( !empty( $announce['range'][$period] ) ) { $add_class .= ' specify'; } printf( '
' , $add_class ); echo '

'; printf( '%s: ' , $period_label ); echo ''; echo '

'; printf( '
' , $period ); echo '

'; echo ''; printf( '%s' , mysql2date( get_option( 'date_format' ) , $announce['date'][$period] ) ); printf( '' , date( 'Y-m-d' , strtotime( $announce['date'][$period] ) ) ); echo '
'; echo ': '; echo ''; printf( '

%1$s: %2$s

' , __( 'Now' , $Afd->ltd ) , mysql2date( get_option( 'date_format' ) . get_option( 'time_format' ) , current_time( 'timestamp' ) ) ); echo '

'; printf( '' , $field_base_name , $period , $announce['date'][$period] ); echo '
'; echo '
'; } } elseif( $field_type == 'user_role' ) { printf( ''; printf( '

%s

' , __( 'Hold the CTRL key and click the items in a list to choose them.' , $Afd->ltd ) ); } elseif( $field_type == 'show_standard' ) { printf( ''; } elseif( $field_type == 'subsites' ) { $add_class = $announce['standard']; printf( '
' , $add_class ); printf( '

%s

' , __( 'Choose the site if you want to hide announce.' , $Afd->ltd ) ); printf( '

%s

' , __( 'Choose the site if you want to show announce.' , $Afd->ltd ) ); echo '
'; printf( ''; printf( '

%s

' , __( 'Hold the CTRL key and click the items in a list to choose them.' , $Afd->ltd ) ); } } public function admin_ajax() { global $Afd; add_action( 'wp_ajax_' . $Afd->ltd . '_announce_validate_data' , array( $this , 'ajax_announce_validate_data' ) ); add_action( 'wp_ajax_' . $Afd->ltd . '_announce_update_sort' , array( $this , 'ajax_announce_update_sort' ) ); } public function ajax_announce_validate_data() { global $Afd; if( empty( $_POST ) ) return false; $nonce_key = $Afd->Form->nonce . 'announce_validate_data'; if( empty( $_POST[$nonce_key] ) ) return false; check_ajax_referer( $nonce_key , $nonce_key ); if( empty( $_POST['data'] ) ) return false; $errors = $this->MainModel->validate_data( $_POST['data'] ); $error_codes = $errors->get_error_codes(); if( !empty( $error_codes ) ) { $return_errors = array(); foreach( $error_codes as $code ) { $return_errors[$code] = array( 'msg' => $errors->get_error_message( $code ) , 'data' => $errors->get_error_data( $code ) ); } wp_send_json_error( array( 'errors' => $return_errors ) ); } else { wp_send_json_success( array( true ) ); } die(); } public function ajax_announce_update_sort() { global $Afd; if( empty( $_POST ) ) return false; $nonce_key = $Afd->Form->nonce . 'announce_update_sort'; if( empty( $_POST[$nonce_key] ) ) return false; check_ajax_referer( $nonce_key , $nonce_key ); if( empty( $_POST['sort_lists'] ) ) return false; $sort_lists = array(); $not_flag = false; foreach( $_POST['sort_lists'] as $sort_id ) { if( is_array( $sort_id ) ) { $not_flag = true; } else { $sort_lists[] = intval( $sort_id ); } } if( empty( $sort_lists ) or $not_flag ) { $return_errors = array(); $return_errors['not_sorted'] = array( 'msg' => 'Not sorted' ); wp_send_json_error( array( 'errors' => $return_errors ) ); } $errors = $settings_data = $this->MainModel->update_sort_datas( $sort_lists ); $error_codes = $errors->get_error_codes(); if( !empty( $error_codes ) ) { $return_errors = array(); foreach( $error_codes as $code ) { $return_errors[$code] = array( 'msg' => $errors->get_error_message( $code ) , 'data' => $errors->get_error_data( $code ) ); } wp_send_json_error( array( 'errors' => $return_errors ) ); } else { wp_send_json_success( array( true ) ); } die(); } } new Afd_Admin_Controller_Announce_Setting(); endif;