Rest_Store_Settings


Source

File: includes/libs/rest-api/endpoints/plugin/rest-store-settings.class.php

018class Rest_Store_Settings
019    extends Rest_Controller_Abstract
020    implements Rest_Controller_Interface {
021 
022    public function __construct(){
023        parent::set_namespace( 'vimeotheque/v1');
024        parent::set_rest_base( '/plugin/settings');
025        $this->register_routes();
026    }
027 
028    /**
029     * @inheritDoc
030     */
031    public function get_response( \WP_REST_Request $request ) {
032        $options = $this->process_options(
033            Plugin::instance()->get_options_obj()->get_defaults(),
034            Plugin::instance()->get_options_obj()->get_options(),
035            $request->get_params()
036        );
037        // Update the options
038        Plugin::instance()->get_options_obj()->update_options( $options );
039 
040        $player_options = $this->process_options(
041            Plugin::instance()->get_embed_options_obj()->get_defaults(),
042            Plugin::instance()->get_embed_options_obj()->get_options(),
043            $request->get_params()
044        );
045        // Update the player embed options
046        Plugin::instance()->get_embed_options_obj()->update_options( $player_options );
047 
048        // Refresh the options.
049        $options = Plugin::instance()->get_options_obj()->get_options(true);
050 
051        $settings_page_url = sprintf(
052            'edit.php?post_type=%s&page=%s',
053            Plugin::instance()->get_cpt()->get_post_type(),
054            'cvm_settings'
055        );
056 
057        $oauth = new Vimeo_Oauth(
058            $options['vimeo_consumer_key'],
059            $options['vimeo_secret_key'],
060            $options['oauth_token'],
061            admin_url( $settings_page_url )
062        );
063 
064        $success = false;
065        $message = '';
066 
067        if( !empty( $options['vimeo_consumer_key'] ) && !empty( $options['vimeo_secret_key'] ) ){
068            if( empty( $options['oauth_token'] ) || ( isset( $options['oauth_secret'] ) && empty( $options['oauth_secret'] ) ) ){
069                $token = $oauth->get_unauth_token();
070 
071                if( !is_wp_error( $token ) ){
072                    $options['oauth_token'] = $token;
073                    $success = true;
074                    $message = __( 'Your Vimeo keys are successfully installed. You can now query public videos on Vimeo and import them as WordPress posts.', 'codeflavors-vimeo-video-post-lite' );
075                }else{
076                    $options['vimeo_consumer_key'] = '';
077                    $options['vimeo_secret_key'] = '';
078                    $options['oauth_token'] = '';
079                    $message = $token->get_error_message();
080                }
081 
082                Plugin::instance()->get_options_obj()->update_options( $options );
083            }
084        }else{
085            $message = __( 'In order to be able to query Vimeo you must enter your Vimeo OAuth client and secret key.', 'codeflavors-vimeo-video-post-lite' );
086        }
087 
088        wp_send_json([
089            'success' => $success,
090            'message' => $message
091        ]);
092    }
093 
094    /**
095     * Process options.
096     *
097     * @param array $defaults       The default options.
098     * @param array $options        The existing options.
099     * @param array $new_options    The new options.
100     *
101     * @return array    The processed options.
102     */
103    private function process_options( $defaults, $options, $new_options ){
104        foreach ( $defaults as $index => $value ) {
105            if( isset( $new_options[ $index ] ) ){
106                switch( gettype( $value ) ){
107                    case 'boolean':
108                        $options[ $index ] = (bool) $new_options[ $index ];
109                    break;
110                    case 'integer':
111                        $options[ $index ] = (int) $new_options[ $index ];
112                    break;
113                    default:
114                        $options[ $index ] = trim( (string) $new_options[ $index ] );
115                    break;
116                }
117            }
118        }
119 
120        return $options;
121    }
122 
123    /**
124     * @inheritDoc
125     */
126    public function register_routes() {
127        register_rest_route(
128            parent::get_namespace(),
129            parent::get_rest_base(),
130            [
131                'methods' => \WP_REST_Server::CREATABLE,
132                'callback' => [$this, 'get_response'],
133                'permission_callback' => function(){
134                    return current_user_can( 'manage_options' );
135                },
136                'args' => [
137 
138                ]
139            ]
140        );
141    }
142}

Methods

Start your video site now!

Manage and coordinate your Vimeo channels, albums or videos with your WordPress website. Perfect fit for membership, portfolio, online courses or any type of video collection.

Get Vimeotheque PRO!