Skip to content

Instantly share code, notes, and snippets.

@glaubersilva
Last active June 12, 2020 16:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save glaubersilva/a475e76a9dde59b6eb9e9da58520162d to your computer and use it in GitHub Desktop.
Save glaubersilva/a475e76a9dde59b6eb9e9da58520162d to your computer and use it in GitHub Desktop.
[Forminator] WP Storys Simple Integration
<?php
/**
* Plugin Name: [Forminator] WP Storys Simple Integration
* Plugin URI: https://premium.wpmudev.org/
* Description: A custom simple integration with WP Storys - more details here >> https://wordpress.org/support/topic/story-form/#post-12957270
* Author: Glauber Silva @ WPMUDEV
* Author URI: https://premium.wpmudev.org/
* Task: FOR-471
* License: GPLv2 or later
*
* @package WPMUDEV_Forminator_WP_Storys_Simple_Integration
*/
defined( 'ABSPATH' ) || exit;
if ( ! class_exists( 'WPMUDEV_Forminator_Storys' ) ) {
/**
* Main class of the plugin.
*/
class WPMUDEV_Forminator_Storys {
/**
* Stores the id of the story.
*
* @var Property
*/
private $story_id = 0;
/**
* Upload Field.
*
* @var Property
*/
private $upload_field = null;
/**
* Text Field.
*
* @var Property
*/
private $text_field = null;
/**
* URL Field.
*
* @var Property
*/
private $url_field = null;
/**
* Stores the main instance of the class.
*
* @var Property
*/
private static $instance = null;
/**
* Returns the main instance of the class.
*
* @return WPMUDEV_Forminator_Storys
*/
public static function get_instance() {
if ( is_null( self::$instance ) ) {
self::$instance = new WPMUDEV_Forminator_Storys();
}
return self::$instance;
}
/**
* Constructor of the class.
*/
private function __construct() {
$this->init();
}
/**
* Loads the functions of the class in the apropriete hooks.
*/
public function init() {
add_action( 'forminator_post_data_field_post_saved', array( $this, 'wpmudev_get_story_data' ), 10, 4 );
add_filter( 'forminator_custom_form_mail_data', array( $this, 'wpmudev_handle_forminator_story' ), 10, 3 );
}
/**
* Gets data related with the story submitted by the user.
*/
public function wpmudev_get_story_data( $post_id, $field, $data, $postdata ) {
if ( 'wp-story' === $field['post_type'] ) {
$this->story_id = $post_id;
}
foreach ( $data['post-custom'] as $field ) {
if ( 'forminator_story_text' === $field['key'] ) {
$this->text_field = $field['value'];
}
if ( 'forminator_story_url' === $field['key'] ) {
$this->url_field = $field['value'];
}
if ( 'forminator_story_img' === $field['key'] ) {
$this->upload_field = $field['value'];
}
}
}
/**
* Updates custom fields of the story submitted by Forminator - This filter is present in: plugins/forminator/library/modules/custom-forms/front/front-mail.php
*
* @since 1.0.4
*
* @param array $data - the post data.
* @param Forminator_Custom_Form_Model $custom_form - the form.
* @param Forminator_Form_Entry_Model $entry - saved entry @since 1.0.3.
*
* @return array $data
*/
public function wpmudev_handle_forminator_story( $data, $custom_form, $entry ) {
$upload_fields = $custom_form->get_fields_by_type( 'upload' );
if ( $upload_fields ) {
foreach ( $upload_fields as $field ) {
$file = $entry->get_meta( $field->slug );
if ( ! empty( $file['file'] ) && $field->slug === $this->upload_field ) {
$img = $file['file']['file_url'];
update_post_meta( $this->story_id, 'forminator_story_img', $img );
$story_items = [];
$story_items[] = [
'text' => sanitize_text_field( $this->text_field ),
'link' => esc_url_raw( $this->url_field ),
'image' => esc_url_raw( $img ),
'new_tab' => sanitize_text_field( 'on' ),
];
update_post_meta( $this->story_id, 'wp_story_items', $story_items );
// Deletes custom fields used by Forminator.
delete_post_meta( $this->story_id, 'forminator_story_text' );
delete_post_meta( $this->story_id, 'forminator_story_url' );
delete_post_meta( $this->story_id, 'forminator_story_img' );
// Automatically add the story to be displayed.
$displayed_stories = get_option( 'wp-story_stories' );
if ( ! $displayed_stories ) {
$displayed_stories = array();
}
array_push( $displayed_stories, $this->story_id );
update_option( 'wp-story_stories', $displayed_stories );
}
}
}
return $data;
}
}
add_action(
'plugins_loaded',
function() {
return WPMUDEV_Forminator_Storys::get_instance();
}
);
}
{"type":"form","data":{"fields":[{"id":"postdata-1","element_id":"postdata-1","form_id":"wrapper-6402-7897","type":"postdata","options":[{"label":"forminator_story_text","value":" {text-1}"},{"label":"forminator_story_url","value":" {url-1}"},{"label":"forminator_story_img","value":"upload-1"}],"cols":12,"conditions":[],"wrapper_id":"wrapper-6402-7897","data_status":"publish","post_title_label":"Post Title","post_content_label":"Post Content","post_excerpt_label":"Post Excerpt","post_image_label":"Featured Image","category_label":"Category","post_tag_label":"Tags","select_author":1,"category_multiple":"0","post_tag_multiple":"0","post_type":"wp-story","post_title":true,"post_image":true,"post_custom_fields":true,"value":"upload-1"},{"id":"text-1","element_id":"text-1","form_id":"wrapper-131-813","type":"text","options":[],"cols":12,"conditions":[],"wrapper_id":"wrapper-131-813","input_type":"line","limit_type":"characters","field_label":"Text","placeholder":"E.g. text placeholder"},{"id":"url-1","element_id":"url-1","form_id":"wrapper-5118-7359","type":"url","options":[],"cols":12,"conditions":[],"wrapper_id":"wrapper-5118-7359","field_label":"Website","placeholder":"E.g. http:\/\/www.example.com"},{"id":"upload-1","element_id":"upload-1","form_id":"wrapper-9076-3257","type":"upload","options":[],"cols":12,"conditions":[],"wrapper_id":"wrapper-9076-3257","field_label":"Upload file","filetypes":["jpg|jpeg|jpe","gif","png","bmp","tiff|tif","ico","asf|asx","wmv","wmx","wm","avi","divx","flv","mov|qt","mpeg|mpg|mpe","mp4|m4v","ogv","webm","mkv","3gp|3gpp","3g2|3gp2","txt|asc|c|cc|h|srt","csv","tsv","ics","rtx","css","htm|html","vtt","dfxp","mp3|m4a|m4b","aac","ra|ram","wav","ogg|oga","flac","mid|midi","wma","wax","mka","rtf","js","pdf","class","tar","zip","gz|gzip","rar","7z","psd","xcf","doc","pot|pps|ppt","wri","xla|xls|xlt|xlw","mdb","mpp","docx","docm","dotx","dotm","xlsx","xlsm","xlsb","xltx","xltm","xlam","pptx","pptm","ppsx","ppsm","potx","potm","ppam","sldx","sldm","onetoc|onetoc2|onetmp|onepkg","oxps","xps","odt","odp","ods","odg","odc","odb","odf","wp|wpd","key","numbers","pages"],"upload-limit":8,"filesize":"MB"}],"settings":{"pagination-header":"nav","paginationData":{"pagination-header-design":"show","pagination-header":"nav"},"formName":"WP Storys","version":"1.12.1.1","form-border-style":"none","form-padding":"","form-border":"","fields-style":"open","validation":"on_submit","form-style":"default","enable-ajax":"true","autoclose":"true","submission-indicator":"show","indicator-label":"Submitting...","form-type":"default","submission-behaviour":"behaviour-thankyou","thankyou-message":"Thank you for contacting us, we will be in touch shortly.","submitData":{"custom-submit-text":"Send Message","custom-invalid-form-message":"Error: Your form is not valid, please fix the errors!"},"validation-inline":"1","form-expire":"no_expire","form-padding-top":"0","form-padding-right":"0","form-padding-bottom":"0","form-padding-left":"0","form-border-width":"0","form-border-radius":"0","cform-label-font-family":"Roboto","cform-label-custom-family":"","cform-label-font-size":"12","cform-label-font-weight":"bold","cform-title-font-family":"Roboto","cform-title-custom-family":"","cform-title-font-size":"45","cform-title-font-weight":"normal","cform-title-text-align":"left","cform-subtitle-font-family":"Roboto","cform-subtitle-custom-font":"","cform-subtitle-font-size":"18","cform-subtitle-font-weight":"normal","cform-subtitle-text-align":"left","cform-input-font-family":"Roboto","cform-input-custom-font":"","cform-input-font-size":"16","cform-input-font-weight":"normal","cform-radio-font-family":"Roboto","cform-radio-custom-font":"","cform-radio-font-size":"14","cform-radio-font-weight":"normal","cform-select-font-family":"Roboto","cform-select-custom-family":"","cform-select-font-size":"16","cform-select-font-weight":"normal","cform-multiselect-font-family":"Roboto","cform-multiselect-custom-font":"","cform-multiselect-font-size":"16","cform-multiselect-font-weight":"normal","cform-dropdown-font-family":"Roboto","cform-dropdown-custom-font":"","cform-dropdown-font-size":"16","cform-dropdown-font-weight":"normal","cform-calendar-font-family":"Roboto","cform-calendar-custom-font":"","cform-calendar-font-size":"13","cform-calendar-font-weight":"normal","cform-button-font-family":"Roboto","cform-button-custom-font":"","cform-button-font-size":"14","cform-button-font-weight":"500","cform-timeline-font-family":"Roboto","cform-timeline-custom-font":"","cform-timeline-font-size":"12","cform-timeline-font-weight":"normal","cform-pagination-font-family":"","cform-pagination-custom-font":"","cform-pagination-font-size":"16","cform-pagination-font-weight":"normal","payment_require_ssl":"","submission-file":"delete","form_status":"publish","form_name":"wp-storys"},"client_id":null,"notifications":[]},"status":"publish","version":"1.12.1.1"}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment