PHP code example of alleyinteractive / wp-video-sync

1. Go to this page and download the library: Download alleyinteractive/wp-video-sync library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

alleyinteractive / wp-video-sync example snippets


use Alley\WP\WP_Video_Sync\Adapters\JW_Player_7_For_WP;
use Alley\WP\WP_Video_Sync\Sync_Manager;
use DateInterval;
use DateTimeImmutable;
use WP_Query;

add_action( 'plugins_loaded', function () => {
	$sync_manager = Sync_Manager::init()
		->with_adapter( new JW_Player_7_For_WP() )
		->with_frequency( 'hourly' )
		->with_batch_size( 1000 )
		->with_callback(
			function ( $video ) {
				$existing_video = new WP_Query( [ 'meta_key' => '_jwppp-video-url-1', 'meta_value' => $video->id ] );
				$existing_id    = $existing_video->posts[0]->ID ?? 0;
				$duration       = '';
				try {
					if ( ! empty( $video->metadata->duration ) ) {
						$duration = ( new DateTimeImmutable() )
							->add( new DateInterval( sprintf( 'PT%dS', (int) $video->metadata->duration ) ) )
							->diff( new DateTimeImmutable() )->format( 'H:i:s' );
					}
				} catch ( Exception $e ) {
					$duration = '';
				}
				wp_insert_post(
					[
						'ID'            => $existing_id,
						'post_type'     => 'post',
						'post_status'   => 'publish',
						'post_title'    => $video->metadata->title,
						'post_content'  => $video->metadata->description ?? '',
						'post_date'     => DateTimeImmutable::createFromFormat( DATE_W3C, $video->created )->format( 'Y-m-d H:i:s' ),
						'post_modified' => DateTimeImmutable::createFromFormat( DATE_W3C, $video->last_modified )->format( 'Y-m-d H:i:s' ),
						'meta_input'    => [
							'_jwppp-video-url-1'           => $video->id,
							'_jwppp-cloud-playlist-1'      => 'no',
							'_jwppp-sources-number-1'      => 1,
							'_jwppp-video-title-1'         => $video->metadata->title,
							'_jwppp-video-description-1'   => $video->metadata->description ?? '',
							'_jwppp-activate-media-type-1' => 0,
							'_jwppp-playlist-carousel-1'   => 0,
							'_jwppp-video-duration-1'      => $duration,
							'_jwppp-video-tags-1'          => $video->metadata->tags ?? '',
						],
					]
				);
			}
		);
} );