class ShopProduct {
private $title;
private $producerMainName;
private $producerFirstName;
protected $price;
private $discount = 0;
public function __construct( string $title, string $firstName, string $mainName, float $price ) {
$this->title = $title;
$this->producerFirstName = $firstName;
$this->producerMainName = $mainName;
$this->price = $price;
}
public function getProducerFirstName() {
return $this->producerFirstName;
}
public function getProducerMainName() {
return $this->producerMainName;
}
public function setDiscount($num) {
$this->discount = $num;
}
public function getDiscount() {
return $this->discount;
}
public function getTitle() {
return $this->title;
}
public function getPrice() {
return ($this->price - $this->discount);
}
public function getProducer() {
return $this->producerFirstName .
. $this->producerMainName;
}
public function getSummaryLine() {
$base = "{$this->title} ( {$this->producerMainName}, ";
$base .= "{$this->producerFirstName} )";
return $base;
}
}
class CdProduct extends ShopProduct {
private $playLength;
public function __construct(string $title, string $firstName, string $mainName, float $price, int $playLength) {
parent::__construct($title, $firstName, $mainName, $price) {
$this->playLength = $playLength;
}
}
public function getPlayLength(){
return $this->playLength;
}
public function getSummaryLine() {
$base = "{$this->title} ( {$this->productMainName}, ";
$base .= "{$this->productFirstName} )";
$base .= ": Время звучания - {$this->playLength}";
return $base;
}
}
class BookProduct extends ShopProduct {
private $numPages;
public function __construct(string $title, string $firstName, string $mainName, float $price, int $numPages) {
parent:: __construct($title, $firstName, $mainName, $price) {
$this->numPages = $numPages;
}
}
public function getNumberOfPages() {
return $this->numPages;
}
public function getSummaryLine() {
$base = parent::getSummaryLine();
$base .= ": {$this->numPages} стр.";
return $base;
}
public function getPrice() {
return $this->price;
}
}
class WDSP_Event_Form {
protected static $instance = null;
public static function get_instance() {
if ( null === self::$instance ) {
self::$instance = new self;
}
return self::$instance;
}
public function __construct() {
$this->hooks();
}
public function hooks() {
add_filter( 'gform_after_submission', array( $this, 'save_gravity_form_event_registration_file_to_media_library' ), 10, 2 );
}
public function save_gravity_form_event_registration_file_to_media_library( $entry, $form ) {
if ( 1234 !== $form['id'] ) {
return;
}
if ( ! isset( $entry['post_id'] ) ) {
return;
}
$gf_field_id = 38;
$acf_field_id = 'field_57bb39fca15a9b';
$post = get_post( $entry['post_id'] );
$file_url = $entry[ $gf_field_id ];
$relative_file_path = parse_url( $file_url, PHP_URL_PATH );
$absolute_file_path = untrailingslashit( ABSPATH ) . $relative_file_path;
$file_type = wp_check_filetype( $absolute_file_path )['type'];
$timeout_seconds = 5;
$sideload_result = $this->sideload_media_file( $file_url, $file_type, $timeout_seconds );
if ( ! $sideload_result || ! empty( $sideload_result['error'] ) ) {
return;
}
$new_file_path = $sideload_result['file'];
$new_file_type = $sideload_result['type'];
$attachment_id = $this->insert_media_file( $new_file_path, $new_file_type );
if ( $attachment_id ) {
update_field( $acf_field_id, $attachment_id, $post->ID );
}
unlink( $absolute_file_path );
}
private function sideload_media_file( $file_url, $file_type, $timeout_seconds ) {
require_once( ABSPATH . 'wp-admin/includes/file.php' );
$temp_file = download_url( $file_url, $timeout_seconds );
if ( is_wp_error( $temp_file ) ) {
return false;
}
$file = array(
'name' => basename( $file_url ),
'type' => $file_type,
'tmp_name' => $temp_file,
'error' => 0,
'size' => filesize( $temp_file ),
);
$overrides = array(
'test_form' => false,
'test_size' => true,
'test_upload' => true,
);
return wp_handle_sideload( $file, $overrides );
}
private function insert_media_file( $file_path = '', $file_type = '' ) {
if ( ! $file_path || ! $file_type ) {
return false;
}
$wp_upload_dir = wp_upload_dir();
$attachment_data = array(
'guid' => $wp_upload_dir['url'] . '/' . basename( $file_path ),
'post_mime_type' => $file_type,
'post_title' => preg_replace( '/\.[^.]+$/', '', basename( $file_path ) ),
'post_content' => '',
'post_status' => 'inherit',
);
$inserted_attachment_id = wp_insert_attachment( $attachment_data, $file_path );
$inserted_attachment_path = get_attached_file( $inserted_attachment_id );
$this->update_inserted_attachment_metadata( $inserted_attachment_id, $inserted_attachment_path );
return $inserted_attachment_id;
}
private function update_inserted_attachment_metadata( $inserted_attachment_id, $file_path ) {
require_once( ABSPATH . 'wp-admin/includes/image.php' );
$attach_data = wp_generate_attachment_metadata( $inserted_attachment_id, $file_path );
wp_update_attachment_metadata( $inserted_attachment_id, $attach_data );
}
}
$wdsp_event_form = WDSP_Event_Form::get_instance();