\d+)/conditions', array( array( 'methods' => \WP_REST_Server::READABLE, 'callback' => array( $this, 'get_conditions' ), 'permission_callback' => array( $this, 'can_edit_template' ), 'args' => array( 'id' => array( 'type' => 'integer', 'required' => true, 'sanitize_callback' => 'absint', ), ), ), array( 'methods' => \WP_REST_Server::EDITABLE, 'callback' => array( $this, 'save_conditions' ), 'permission_callback' => array( $this, 'can_edit_template' ), 'args' => array( 'id' => array( 'type' => 'integer', 'required' => true, 'sanitize_callback' => 'absint', ), 'conditions' => array( 'type' => 'array', 'default' => array(), ), 'visibility' => array( 'type' => 'object', 'default' => array(), ), 'priority' => array( 'type' => 'integer', 'default' => 0, 'minimum' => -100, 'maximum' => 100, 'sanitize_callback' => static function ( $value ) { return (int) $value; }, ), ), ), ) ); register_rest_route( self::NAMESPACE_V1, '/conditions/search', array( 'methods' => \WP_REST_Server::READABLE, 'callback' => array( $this, 'search_objects' ), 'permission_callback' => array( $this, 'can_manage' ), 'args' => array( 'object' => array( 'type' => 'string', 'required' => true, 'enum' => array( 'post', 'term', 'author' ), ), 'sub' => array( 'type' => 'string', 'default' => '', 'sanitize_callback' => 'sanitize_key', ), 'search' => array( 'type' => 'string', 'default' => '', 'sanitize_callback' => 'sanitize_text_field', ), ), ) ); register_rest_route( self::NAMESPACE_V1, '/popups/(?P\d+)/track', array( 'methods' => \WP_REST_Server::CREATABLE, 'callback' => array( $this, 'track' ), // Public endpoint: visitors are anonymous and pages may be cached, so no nonce is used. // Only aggregate counters of published popups can change; requests are rate limited. 'permission_callback' => '__return_true', 'args' => array( 'id' => array( 'type' => 'integer', 'required' => true, 'sanitize_callback' => 'absint', ), 'event' => array( 'type' => 'string', 'required' => true, 'enum' => Popup_Stats::EVENTS, ), ), ) ); } /** * Permission: edit a template. * * @param \WP_REST_Request $request Request. * @return bool */ public function can_edit_template( $request ) { $id = (int) $request['id']; return $id && Post_Type::POST_TYPE === get_post_type( $id ) && current_user_can( 'edit_post', $id ); } /** * Permission: manage templates. * * @return bool */ public function can_manage() { return current_user_can( Post_Type::capability() ); } /** * Returns the conditions of a template. * * @param \WP_REST_Request $request Request. * @return \WP_REST_Response */ public function get_conditions( $request ) { $id = (int) $request['id']; $data = Conditions::get_template_data( $id ); $conditions = array(); foreach ( $data['conditions'] as $rule ) { $rule['title'] = ! empty( $rule['id'] ) ? Conditions::object_title( $rule ) : ''; $conditions[] = $rule; } return rest_ensure_response( array( 'conditions' => $conditions, 'visibility' => $data['visibility'], 'priority' => (int) get_post_meta( $id, Post_Type::PRIORITY_META, true ), ) ); } /** * Saves the conditions of a template. * * @param \WP_REST_Request $request Request. * @return \WP_REST_Response */ public function save_conditions( $request ) { $id = (int) $request['id']; Conditions::save_template_data( $id, $request['conditions'], $request['visibility'], (int) $request['priority'] ); return $this->get_conditions( $request ); } /** * Searches posts, terms or authors for the conditions editor. * * @param \WP_REST_Request $request Request. * @return \WP_REST_Response */ public function search_objects( $request ) { $object = $request['object']; $sub = $request['sub']; $search = $request['search']; $results = array(); if ( 'post' === $object ) { // Only public post types and published content: display conditions target pages visitors can see. $public_types = array_keys( Helpers::get_post_types() ); $post_type = in_array( $sub, $public_types, true ) ? $sub : $public_types; $posts = get_posts( array( 'post_type' => $post_type, 'post_status' => 'publish', 'has_password' => false, 's' => $search, 'posts_per_page' => 20, 'orderby' => '' === $search ? 'date' : 'relevance', 'suppress_filters' => false, 'no_found_rows' => true, ) ); foreach ( $posts as $post ) { if ( ! current_user_can( 'read_post', $post->ID ) ) { continue; } $results[] = array( 'id' => $post->ID, 'title' => wp_strip_all_tags( get_the_title( $post ) ) . ' (#' . $post->ID . ')', ); } } elseif ( 'term' === $object ) { // Only public taxonomies. if ( array_key_exists( $sub, Helpers::get_taxonomies() ) ) { $terms = get_terms( array( 'taxonomy' => $sub, 'search' => $search, 'number' => 20, 'hide_empty' => false, ) ); if ( ! is_wp_error( $terms ) ) { foreach ( $terms as $term ) { $results[] = array( 'id' => $term->term_id, 'title' => $term->name . ' (#' . $term->term_id . ')', ); } } } } elseif ( current_user_can( 'list_users' ) ) { // Authors: only for users who may list users anyway. $users = get_users( array( 'search' => '' !== $search ? '*' . $search . '*' : '', 'search_columns' => array( 'display_name', 'user_login', 'user_nicename' ), 'number' => 20, 'capability' => array( 'edit_posts' ), 'fields' => array( 'ID', 'display_name' ), ) ); foreach ( $users as $user ) { $results[] = array( 'id' => (int) $user->ID, 'title' => $user->display_name, ); } } return rest_ensure_response( $results ); } /** * Records a popup event. * * @param \WP_REST_Request $request Request. * @return \WP_REST_Response */ public function track( $request ) { $id = (int) $request['id']; $event = (string) $request['event']; if ( ! Popup_Stats::enabled() ) { return rest_ensure_response( array( 'recorded' => false ) ); } $post = get_post( $id ); if ( ! $post || Post_Type::POST_TYPE !== $post->post_type || 'publish' !== $post->post_status || 'popup' !== Template_Types::of( $id ) ) { return new \WP_REST_Response( array( 'recorded' => false ), 404 ); } // Rate limit per visitor, popup and event. The IP is hashed and only kept in a short-lived transient. $ip = isset( $_SERVER['REMOTE_ADDR'] ) ? sanitize_text_field( wp_unslash( $_SERVER['REMOTE_ADDR'] ) ) : ''; $key = 'buildify_rl_' . md5( $ip . '|' . $id . '|' . $event . '|' . wp_salt( 'nonce' ) ); if ( get_transient( $key ) ) { return rest_ensure_response( array( 'recorded' => false ) ); } set_transient( $key, 1, 10 ); return rest_ensure_response( array( 'recorded' => Popup_Stats::record( $id, $event ) ) ); } }