Plugin

Class Plugin


Source

File: includes/libs/plugin.class.php

029class Plugin{
030 
031    /**
032    * Holds the plugin instance.
033    *
034    * @var Plugin
035    */
036    private static $instance = null;
037    /**
038     * Stores plugin options
039     *
040     * @var Options
041     */
042    private $options;
043    /**
044     * Stores player options
045     *
046     * @var Options
047     */
048    private $player_options;
049    /**
050     * @var Post_Type
051     */
052    private $cpt;
053    /**
054     * Store admin instance
055     * @var Admin
056     */
057    private $admin;
058    /**
059     * @var Posts_Import
060     */
061    private $posts_import;
062    /**
063     * @var Front_End
064     */
065    private $front_end;
066    /**
067     * @var Blocks_Factory
068     */
069    private $blocks_factory;
070    /**
071     * @var Themes
072     */
073    private $playlist_themes;
074    /**
075     * @var Post_Registration
076     */
077    private $registered_post_types;
078    /**
079     * @var Customizer
080     */
081    private $customizer;
082 
083    /**
084     * @var Rest_Api
085     */
086    private $rest_api;
087 
088    /**
089     * Clone.
090     *
091     * Disable class cloning and throw an error on object clone.
092     *
093     * The whole idea of the singleton design pattern is that there is a single
094     * object. Therefore, we don't want the object to be cloned.
095     *
096     * @access public
097     */
098    public function __clone() {
099        // Cloning instances of the class is forbidden.
100        _doing_it_wrong( __FUNCTION__, esc_html__( 'Something went wrong.', 'codeflavors-vimeo-video-post-lite' ), '2.0' );
101    }
102 
103    /**
104     * Wakeup.
105     *
106     * Disable unserializing of the class.
107     *
108     * @access public
109     */
110    public function __wakeup() {
111        // Unserializing instances of the class is forbidden.
112        _doing_it_wrong( __FUNCTION__, esc_html__( 'Something went wrong.', 'codeflavors-vimeo-video-post-lite' ), '2.0' );
113    }
114 
115    /**
116     * Instance.
117     *
118     * Ensures only one instance of the plugin class is loaded or can be loaded.
119     *
120     * @access public
121     * @static
122     *
123     * @return Plugin
124     */
125    public static function instance() {
126        if ( is_null( self::$instance ) ) {
127            self::$instance = new self();
128 
129            /**
130             * Triggered when Vimeotheque has loaded.
131             * Fires when Vimeotheque was fully loaded and instantiated.
132             *
133             * @since 2.0
134             */
135            do_action( 'vimeotheque_loaded' );
136        }
137 
138        return self::$instance;
139    }
140 
141    /**
142     * Class constructors - sets all filters and hooks
143     */
144    private function __construct(){
145        // start the autoloader
146        $this->register_autoloader();
147        // load dependency files
148        $this->load();
149 
150        // activation hook to add the rewrite rules for the custom post type
151        register_activation_hook( VIMEOTHEQUE_FILE, [
152            $this,
153            'activation_hook'
154        ] );
155 
156        add_action( 'plugins_loaded', [
157            $this,
158            'init'
159        ], 1 );
160 
161        // run this admin init on init to have access to earlier hooks
162        // priority must be set to run very early so that init hooks set
163        // in admin page can also run
164        add_action( 'init', [
165            $this,
166            'admin_init'
167        ], -9999999 );
168 
169        new Amp();
170        new Templates_Init();
171 
172        add_action(
173            'after_setup_theme',
174            function(){
175                $options = $this->get_options();
176                if( $options['enable_templates'] && !current_theme_supports( 'vimeotheque' ) ){
177                    /**
178                     * Support for Video templates.
179                     */
180                    add_theme_support( 'vimeotheque' );
181 
182                    /**
183                     * Support for next video card after the video finishes.
184                     */
185                    add_theme_support( 'vimeotheque-next-video-card' );
186                }
187            }, 1
188        );
189 
190        add_filter(
191            'vimeotheque\options\get',
192            /**
193             * Filter options.
194             *
195             * Filter the plugin options and check if templates are enabled.
196             *
197             * @param array $result             The requested options set.
198             * @param array $all_options        All the plugin options.
199             * @param string $wp_option_name    The WP option name.
200             */
201            function( $result, $all_options, $wp_option_name ){
202 
203                if( $this->get_options_obj()->get_option_name() == $wp_option_name ){
204 
205                    if( get_theme_support( 'vimeotheque' ) ){
206                        $all_options['enable_templates'] = true;
207                        if( isset( $result['enable_templates'] ) ) {
208                            $result['enable_templates'] = true;
209                        }
210                    }
211 
212                    if( $all_options['enable_templates'] ) {
213                        // When templates are enabled, these options will always have the same predefined value.
214                        $options = [
215                            'archives'           => false,
216                            'public'             => true,
217                            'import_title'       => true,
218                            'import_description' => 'content',
219                            'featured_image'      => true
220                        ];
221 
222                        // Set the options to the predefined value.
223                        foreach ( $options as $index => $value ) {
224                            if ( isset( $result[ $index ] ) ) {
225                                $result[ $index ] = $value;
226                            }
227                        }
228                    }
229                }
230 
231                return $result;
232 
233            }, -999, 3
234        );
235    }
236 
237    public function init(){
238        // register the post type
239        $this->set_post_type();
240        // set the importer
241        $this->load_importer();
242        // start the front-end
243        $this->load_front_end();
244 
245        new Shortcode_Factory();
246        $this->blocks_factory = new Blocks_Factory( $this );
247 
248        new Widgets_Factory( $this );
249 
250        $this->playlist_themes = new Themes(
251            new Theme(
252                VIMEOTHEQUE_PATH . 'themes/default/player.php',
253                __( 'Default', 'codeflavors-vimeo-video-post-lite' )
254            )
255        );
256 
257        $this->playlist_themes->register_theme(
258            new Theme(
259                VIMEOTHEQUE_PATH . 'themes/simple/theme.php',
260                __( 'Simple', 'codeflavors-vimeo-video-post-lite' )
261            )
262        );
263 
264        $this->playlist_themes->register_theme(
265            new Theme(
266                VIMEOTHEQUE_PATH . 'themes/listy/theme.php',
267                __( 'Listy', 'codeflavors-vimeo-video-post-lite' )
268            )
269        );
270 
271        // internalization
272        load_plugin_textdomain(
273            'codeflavors-vimeo-video-post-lite',
274            false,
275            basename( dirname( VIMEOTHEQUE_FILE ) ) . '/languages/'
276        );
277    }
278 
279    /**
280     * Register the autoloader
281     */
282    private function register_autoloader(){
283        require VIMEOTHEQUE_PATH . 'includes/libs/autoload.class.php';
284        Autoload::run();
285    }
286 
287    /**
288     * Register the post type
289     */
290    private function set_post_type(){
291        $this->cpt = new Post_Type( $this );
292        add_action( 'init', function(){
293            $this->registered_post_types = new Post_Registration(
294                $this->cpt->get_wp_post_type_object(),
295                $this->cpt->get_category_taxonomy_object(),
296                get_taxonomy( $this->cpt->get_tag_tax() )
297            );
298        }, 2 );
299    }
300 
301    /**
302     * Loads the automatic importer
303     */
304    private function load_importer(){
305        /**
306         * Posts importer filter.
307         *
308         * @param Posts_Import $importer The Posts_Import object reference.
309         */
310        $this->posts_import = apply_filters(
311            'vimeotheque\set_importer',
312            new Posts_Import( $this->get_cpt() )
313        );
314    }
315 
316    /**
317     * Loads the front-end
318     */
319    private function load_front_end(){
320        // start the REST API compatibility
321        $this->rest_api = new Rest_Api( $this->get_cpt() );
322        // start the front-end functionality
323        $this->front_end = new Front_End( $this );
324    }
325 
326    /**
327     * Set plugin options
328     */
329    private function set_plugin_options(){
330        $defaults = [
331            'enable_templates' => false, // use the video templates for themes
332            'public' => true, // post type is public or not
333            'archives' => false, // display video embed on archive pages
334            'post_slug' => 'vimeo-video',
335            'taxonomy_slug' => 'vimeo-videos',
336            'tag_slug' => 'vimeo-tag',
337            'import_tags' => true, // import tags retrieved from Vimeo
338            'max_tags' => 3, // how many tags to import
339            'import_title' => true, // import titles on custom posts
340            'import_description' => 'content', // import descriptions on custom posts
341            'import_date' => true, // import video date as post date
342            'featured_image' => true, // set thumbnail as featured image; default import on video feed import (takes more time)
343            'import_status' => 'publish', // default import status of videos
344            // Vimeo oAuth
345            'vimeo_consumer_key' => '',
346            'vimeo_secret_key' => '',
347            'oauth_token' => '' // retrieved from Vimeo; gets set after entering valid client ID and client secret
348        ];
349 
350        /**
351         * Options filter.
352         *
353         * @param array $defaults Default options array.
354         */
355        $defaults = apply_filters(
356            'vimeotheque\options_default',
357            $defaults
358        );
359 
360        $this->options = Options_Factory::get( '_cvm_plugin_settings', $defaults );
361    }
362 
363    /**
364     * Set video player options
365     */
366    private function set_player_options(){
367        $defaults = [
368            'title' => 1,    // show video title
369            'byline' => 1,   // show player controls. Values: 0 or 1
370            'portrait' => 1,     // show author image
371            'loop' => 0,
372            // Autoplay may be blocked in some environments, such as IOS, Chrome 66+, and Safari 11+. In these cases, we’ll revert to standard playback requiring viewers to initiate playback.
373            'autoplay' => 0,     // 0 - on load, player won't play video; 1 - on load player plays video automatically
374            'color'     => '',   // no color set by default; will use Vimeo's settings
375            'dnt' => 0, // block Vimeo player from tracking session data or cookies (1) or allow it (0);
376            // extra settings
377            'aspect_ratio' => '16x9',
378            'width' => 900,
379            'max_height' => 0, // allows setup of a maximum embed height; must be a value over 50px to work
380            'video_position' => 'below-content', // in front-end custom post, where to display the video: above or below post content
381            'video_align' => 'align-left', // video alignment
382            'lazy_load' => false, // lazy load videos
383            'play_icon_color' => '#FF0000', // lazy load play icon color
384            'volume' => 45, // video default volume
385            // extra player settings controllable by widgets/shortcodes
386            'playlist_loop' => 0,
387            'aspect_override' => true,
388            'start_time' => 0, // time in seconds when playback should start
389            'muted' => false, // load video muted
390            'background' => false, // load video in background mode (hides controls and mutes video)
391            'transparent' => false, // video embed should be with background (false) or without it (true)
392        ];
393 
394        /**
395         * Player options filter.
396         *
397         * @param array $defaults Default options array.
398         */
399        $defaults = apply_filters(
400            'vimeotheque\player_options_default',
401            $defaults
402        );
403 
404        // get Plugin option
405        $this->player_options = Options_Factory::get( '_cvm_player_settings', $defaults );
406    }
407 
408    /**
409     * Runs on plugin activation and registers rewrite rules
410     * for video custom post type
411     *
412     * @return void
413     */
414    public function activation_hook(){
415        $this->set_post_type();
416        // register custom post
417        $this->get_cpt()->register_post();
418        // create rewrite ( soft )
419        flush_rewrite_rules( false );
420 
421        $this->add_admin();
422 
423        $wp_option = get_option( $this->get_options_obj()->get_option_name() );
424        if( !$wp_option ){
425            set_transient( 'vimeotheque_setup_activated' , time(), 30 );
426        }
427    }
428 
429    /**
430     * Returns plugin options array
431     *
432     * @return array
433     */
434    public function get_options(){
435        $options = $this->get_options_obj();
436        return $options->get_options( is_customize_preview() );
437    }
438 
439    /**
440     * Returns options object
441     *
442     * @return Options
443     */
444    public function get_options_obj(){
445        if( !$this->options ){
446            $this->set_plugin_options();
447        }
448 
449        return $this->options;
450    }
451 
452    /**
453     * Returns player options object
454     *
455     * @return Options
456     */
457    public function get_embed_options_obj(){
458        if( !$this->player_options ){
459            $this->set_player_options();
460        }
461 
462        return $this->player_options;
463    }
464 
465    /**
466     * @return array
467     */
468    public function get_embed_options(){
469        $options = $this->get_embed_options_obj();
470        return $options->get_options( is_customize_preview() );
471    }
472 
473    /**
474     * Callback function for hook "admin_init"
475     */
476    public function admin_init(){
477        $this->add_customizer();
478        $this->add_admin();
479    }
480 
481    /**
482     * Implements WP customizer functionality
483     */
484    private function add_customizer(){
485        if( is_admin() || is_customize_preview() ) {
486            $this->customizer = new Customizer();
487 
488        }
489    }
490 
491    /**
492     * Adds plugin administration functionality
493     */
494    private function add_admin(){
495        if( is_admin() || Helper::is_ajax() ) {
496            $this->admin = new Admin( $this->get_cpt() );
497        }
498    }
499 
500    /**
501     * @param bool $cap
502     *
503     * @return array|mixed
504     * @throws \Exception
505     */
506    public function get_capability( $cap = false ){
507        return $this->admin->get_capability( $cap );
508    }
509 
510    /**
511     * @return array
512     */
513    public function get_roles(){
514        return $this->admin->get_roles();
515    }
516 
517    /**
518     * @return Post_Type
519     */
520    public function get_cpt(){
521        return $this->cpt;
522    }
523 
524    /**
525     * @return Posts_Import
526     */
527    public function get_posts_importer(){
528        return $this->posts_import;
529    }
530 
531    /**
532     * Get the admin.
533     *
534     * Returns the Admin object that allows access to various admin sections like the menu
535     * or custom post type definitions.
536     *
537     * @return Admin
538     */
539    public function get_admin(){
540        if( is_null( $this->admin ) ){
541            $this->add_admin();
542        }
543        return $this->admin;
544    }
545 
546    /**
547     * @return Front_End
548     */
549    public function get_front_end(){
550        return $this->front_end;
551    }
552 
553    /**
554     * @return Themes
555     */
556    public function get_playlist_themes() {
557        return $this->playlist_themes;
558    }
559 
560    /**
561     * @param string $key - string key for the block
562     *
563     * @return Block_Abstract - returns the registered block
564     * @see Blocks_Factory::register_blocks() for all keys
565     */
566    public function get_block( $key ) {
567        return $this->blocks_factory->get_block( $key );
568    }
569 
570    /**
571     * @return Blocks_Factory
572     */
573    public function get_blocks(){
574        return $this->blocks_factory;
575    }
576 
577    /**
578     * @return Post_Registration
579     */
580    public function get_registered_post_types(){
581        return $this->registered_post_types;
582    }
583 
584    /**
585     * @return Customizer
586     */
587    public function get_customizer() {
588        return $this->customizer;
589    }
590 
591    /**
592     * @return Rest_Api
593     */
594    public function get_rest_api() {
595        return $this->rest_api;
596    }
597 
598    /**
599     * Load dependencies
600     * @return void
601     */
602    private function load(){
603        add_action( 'vimeotheque_pro_loaded', function(){
604            include_once VIMEOTHEQUE_PATH . 'includes/deprecated.php';
605        } );
606    }
607}

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!