Last active
July 8, 2020 18:58
-
-
Save xnau/3fd3c99dc7d90a57c2f5abe6b52a4749 to your computer and use it in GitHub Desktop.
Shows a way to create a Participants Database form element that displays an embedded video by using WordPress' built-in video embed filters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Plugin Name: PDB Video Form Element | |
* Description: Demonstrates how to create a Participants Database form element that displays a video | |
* Version: 3.0 | |
* Text Domain: pdb-video-form-element | |
* Domain Path: /languages | |
* | |
*/ | |
class PDb_Video_Form_Element { | |
/** | |
* @var string name of the form element | |
*/ | |
private $name = 'video'; | |
/** | |
* | |
*/ | |
public function __construct() | |
{ | |
add_filter( 'pdb-set_form_element_types', array($this, 'video_register_element') ); | |
add_action( 'pdb-form_element_build_' . $this->name, array($this, 'video_element_building_function') ); | |
add_filter( 'pdb-before_display_form_element', array($this, 'video_element_value_display_function'), 10, 2 ); | |
} | |
/** | |
* adds the form element to the list of form elements | |
* | |
* @param array $elements | |
* @return array | |
*/ | |
public function video_register_element( $elements ) | |
{ | |
$elements[$this->name] = __( 'Video', 'pdb-video-form-element' ); | |
return $elements; | |
} | |
/** | |
* supplies the form element HTML in a write context | |
* | |
* @param PDb_FormElement $field | |
* @return null | |
*/ | |
public function video_element_building_function( $field ) | |
{ | |
$field->type = 'text-line'; | |
} | |
/** | |
* displays the video | |
* | |
* this relies on the built-in WP filter that makes an embed out of a URL | |
* | |
* @param array $value the raw value of the field | |
* @param PDb_Field_Item object $field | |
* @return string HTML | |
*/ | |
public function video_element_value_display_function( $value, $field ) | |
{ | |
if ( $field->form_element === $this->name ) { | |
$oembed = new WP_oEmbed(); | |
$value[] = '<div class="pdb-video-container ' . $field->name . '-video">'; | |
$value[] = $oembed->get_html( $this->extract_url( $field->value ) ); | |
$value[] = '</div>'; | |
return implode( PHP_EOL, $value ); | |
} | |
return $value; | |
} | |
/** | |
* extracts the URL from the content | |
* | |
* this is to clean up a messy content string so that all that remains is the URL | |
* | |
* @param string $content | |
* @return string the cleaned-up URL | |
*/ | |
private function extract_url( $content ) | |
{ | |
$output = ''; | |
foreach( explode( ' ', $content ) as $part ) { | |
if ( empty( $part ) ) { | |
continue; | |
} | |
$check = filter_var( $part, FILTER_VALIDATE_URL, FILTER_FLAG_PATH_REQUIRED ); | |
if ( $check !== false ) { | |
$output = $check; | |
break; | |
} | |
} | |
return $output; | |
} | |
} | |
new PDb_Video_Form_Element(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment