文件操作 - SurveyApi.php
返回文件管理
返回主菜单
删除本文件
文件: /home/www/public/wp-content/plugins/ionos-loop/inc/rest/SurveyApi.php
编辑文件内容
<?php namespace Ionos\Loop; use WP_Error; use WP_REST_Request; use WP_REST_Response; // No direct access allowed. if ( ! defined( 'ABSPATH' ) ) : exit(); endif; /** * Class for handling backend calls of the survey system. */ class SurveyApi { const API_NAMESPACE = 'ionos/v1'; const API_ENDPOINT = '/shift'; const SURVEY_ID_PATTERN = '/^[0-9a-fA-F]{8}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-F]{12}$/m'; const OPTION_KEY = 'ionos_loop_survey_answers'; /** * Register rest routes for survey handling */ public function register_rest_route() { register_rest_route( self::API_NAMESPACE, self::API_ENDPOINT, [ [ 'methods' => 'POST', 'callback' => [ $this, 'shift_survey' ], 'permission_callback' => [ $this, 'check_permission' ], ], ] ); } /** * Check if current user is an administrator * * @return bool */ public function check_permission() { if ( is_user_logged_in() ) { return current_user_can( 'manage_options' ); } return false; } /** * Add survey answer to option. * * @param array $data Input data. * * @return WP_Error | WP_REST_Response */ public function shift_survey( $data ) { if ( empty( $data ) ) { return new WP_Error( 'rest_bad_request', esc_html__( 'Data required.' ), [ 'status' => 400 ] ); } if ( ! isset( $data['id'] ) || ! isset( $data['choice'] ) ) { return new WP_Error( 'rest_bad_request', esc_html__( 'Invalid data.' ), [ 'status' => 400 ] ); } // Check if id is a uuid and choice is an integer. if ( preg_match( self::SURVEY_ID_PATTERN, $data['id'] ) !== 1 || is_integer( $data['choice'] ) ) { return new WP_Error( 'rest_bad_request', esc_html__( 'Invalid data.' ), [ 'status' => 400 ] ); } $surveys = get_option( self::OPTION_KEY, [] ); $surveys[ $data['id'] ] = $data['choice']; update_option( self::OPTION_KEY, $surveys ); return rest_ensure_response( new WP_REST_Response( [ 'success' => 'true' ], 200 ) ); } }
修改文件时间
将文件时间修改为当前时间的前一年
删除文件