Start Youtube importer

This commit is contained in:
Eduardo Humberto 2019-04-15 20:47:14 -03:00
parent 820b17b2b8
commit 510b259dc9
2 changed files with 113 additions and 0 deletions

View File

@ -61,6 +61,15 @@ class Importer_Handler {
'manual_mapping' => false,
]);
// $this->register_importer([
// 'name' => 'Youtube (Experimental)',
// 'description' => __('Import items from an Youtube URL', 'tainacan'),
// 'slug' => 'youtube_importer',
// 'class_name' => '\Tainacan\Importer\Youtube_Importer',
// 'manual_collection' => true,
// 'manual_mapping' => false,
// ]);
do_action('tainacan_register_importers');
add_action( 'tainacan-enqueue-admin-scripts', array($this, 'enqueue_scripts') );

View File

@ -0,0 +1,104 @@
<?php
namespace Tainacan\Importer;
use Tainacan;
use Tainacan\Entities;
class Youtube_Importer extends Importer {
protected $steps = [
[
'name' => 'Identify URL',
'progress_label' => 'Identify URL',
'callback' => 'identify_url'
],
[
'name' => 'Import Items',
'progress_label' => 'Import Items',
'callback' => 'process_collections',
'total' => 2
],
];
/**
* construct
*/
public function __construct($attributes = array()) {
parent::__construct($attributes);
$this->col_repo = \Tainacan\Repositories\Collections::get_instance();
$this->items_repo = \Tainacan\Repositories\Items::get_instance();
$this->metadata_repo = \Tainacan\Repositories\Metadata::get_instance();
$this->item_metadata_repo = \Tainacan\Repositories\Item_Metadata::get_instance();
$this->tax_repo = \Tainacan\Repositories\Taxonomies::get_instance();
$this->term_repo = \Tainacan\Repositories\Terms::get_instance();
$this->remove_import_method('file');
$this->add_import_method('url');
}
/**
* Method implemented by the child importer class to proccess each item
* @return int
*/
public function process_item( $index, $collection_id ){
}
/**
* identify the type of url is send by user
*/
public function identify_url(){
$url = $this->get_url();
$preg_entities = [
'channel_id' => '\/channel\/(([^\/])+?)$', //match YouTube channel ID from url
'user' => '\/user\/(([^\/])+?)$', //match YouTube user from url
'playlist_id' => '\/playlist\?list=(([^\/])+?)$', //match YouTube playlist ID from url
'v' => '\'%(?:youtube(?:-nocookie)?\.com/(?:[^/]+/.+/|(?:v|e(?:mbed)?)/|.*[?&]v=)|youtu\.be/)([^"&?/ ]{11})%i\''
];
foreach ( $preg_entities as $key => $preg_entity ) {
if ( preg_match( '/' . $preg_entity . '/', $url, $matches ) ) {
if ( isset( $matches[1] ) ) {
return [
'type' => $key,
'id' => $matches[1],
];
}
}
}
}
public function options_form(){
ob_start();
?>
<div class="field">
<label class="label"><?php _e('API ID', 'tainacan'); ?></label>
<span class="help-wrapper">
<a class="help-button has-text-secondary">
<span class="icon is-small">
<i class="tainacan-icon tainacan-icon-help" ></i>
</span>
</a>
<vdiv class="help-tooltip">
<div class="help-tooltip-header">
<h5><?php _e('Youtube API ID', 'tainacan'); ?></h5>
</div>
<div class="help-tooltip-body">
<p><?php _e('Get youtube API ID if you request for channels or playlists', 'tainacan'); ?></p>
</div>
</vdiv>
</span>
<div class="control is-clearfix">
<input class="input" type="text" name="api_id" value="<?php echo $this->get_option('api_id'); ?>">
</div>
</div>
<?php
return ob_get_clean();
}
}