plugin_file_path ) . '/vendor/polevaultweb/wp-oauth2/wp-oauth2.php'; require_once dirname( NF_File_Uploads()->plugin_file_path ) . '/vendor/polevaultweb/wp-oauth2/access-token.php'; require_once dirname( NF_File_Uploads()->plugin_file_path ) . '/vendor/polevaultweb/wp-oauth2/admin-handler.php'; require_once dirname( NF_File_Uploads()->plugin_file_path ) . '/vendor/polevaultweb/wp-oauth2/oauth2-client.php'; require_once dirname( NF_File_Uploads()->plugin_file_path ) . '/vendor/polevaultweb/wp-oauth2/dropbox-client.php'; add_filter( 'ninja_forms_register_actions', array( $this, 'register_actions' ) ); add_filter( 'ninja_forms_file_uploads_settings_whitelist', array( $this, 'add_settings_to_whitelist' ) ); add_action( 'ninja_forms_uploads_external_template', array( $this, 'render_services_settings' ) ); add_filter( 'ninja_forms_uploads_file_url', array( $this, 'generate_url' ), 10, 2); add_filter( 'ninja_forms_uploads_file_location', array( $this, 'get_service_display_name' ) ); add_action( 'ninja_forms_loaded', array( $this, 'init' ) ); add_action( 'admin_init', array( $this, 'handle_external_url' ) ); add_action( 'template_redirect', array( $this, 'handle_external_url_public' ) ); } public function init() { $class = 'Polevaultweb\\WP_OAuth2\\Admin_Handler'; $admin_handler = new $class( NF_File_Uploads()->page->get_url( 'external', array(), false ) ); $admin_handler->init(); } /** * Is the site compatible for external uploads * * @return bool */ public static function is_compatible() { return version_compare( PHP_VERSION, self::$min_php_version, '>=' ); } /** * Get the external services we can upload files to * * @return array */ public function get_services() { $default_services = glob( dirname( NF_File_Uploads()->plugin_file_path ) . '/includes/external/services/*', GLOB_ONLYDIR ); $default_services = array_map( 'basename', $default_services ); $services = array(); foreach ( $default_services as $service ) { $file = dirname( NF_File_Uploads()->plugin_file_path ) . '/includes/external/services/' . $service . '/service.php'; $services[ $file ] = $service; } /** * External services * * Array of external services, key is the path to service file, value is service name slug * * @return array $services */ return apply_filters( 'ninja_forms_uploads_external_service', $services ); } /** * Get external service * * @param string $service * @param null|array $services * * @return bool|NF_FU_External_Service */ public function get( $service, $services = null ) { $service = $this->get_upload_location( $service ); if ( is_null( $services ) ) { $services = $this->get_services(); } if ( ! in_array( $service, $services ) ) { return false; } $file = array_search( $service, $services ); if ( ! file_exists( $file ) ) { return false; } $service = ucfirst( $service ); $class = $this->class_prefix . $service . '_Service'; if ( ! method_exists( $class, 'instance') ) { return false; } $external = call_user_func( array( $class, 'instance' ) ); return $external; } /** * Are there any services that are ready to use? * * @return bool */ protected function is_connected_services() { $services = $this->get_services(); foreach ( $services as $service ) { if ( ! ( $instance = $this->get( $service, $services ) ) ) { continue; } if ( $instance->is_compatible() && $instance->is_connected() ) { return true; } } return false; } /** * Register actions for external services * * @param array $actions * * @return array */ public function register_actions( $actions ) { if ( $this->is_connected_services() ) { $actions['file-upload-external'] = new NF_FU_External_Action(); } return $actions; } /** * Add the external tab to the settings page * * @param $tabs * * @return mixed */ public function add_menu_tab( $tabs ) { $tabs['external'] = __( 'External Settings', 'ninja-forms-uploads' ); return $tabs; } /** * Add external settings to the whitelist for saving from settings page * * @param array $whitelist * * @return array */ public function add_settings_to_whitelist( $whitelist ) { $services = $this->get_services(); foreach ( $services as $service ) { if ( ! ( $instance = $this->get( $service, $services ) ) ) { continue; } $settings = $instance->get_settings(); $whitelist = array_merge( $whitelist, $settings ); } return $whitelist; } /** * Render the settings for the services on the External settings page */ public function render_services_settings() { $services = $this->get_services(); foreach ( $services as $service ) { if ( ! ( $instance = $this->get( $service, $services ) ) ) { continue; } $requirements = $instance->get_missing_requirements(); if ( $requirements ) { $message = sprintf( __( '%s requires %s', 'ninja-forms-uploads' ), $instance->get_name(), implode( ', ', $requirements ) ); printf( '
%s
%s