Last active
July 14, 2018 00:18
-
-
Save mariovalney/d692127f7ec1cecfe5b0a50f22fb6390 to your computer and use it in GitHub Desktop.
A Helper to help you create Pages, Elementor and ACF Data to be inserted using installers
This file contains hidden or 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 | |
| /** | |
| * VZR_Updater_Helper | |
| * Helper to VZR_Module_Updater | |
| * | |
| * @package VZR_Example | |
| * @subpackage VZR_Module_Updater | |
| * @since 1.0.0 | |
| * | |
| */ | |
| // If this file is called directly, call the cops. | |
| defined( 'ABSPATH' ) or die( 'No script kiddies please!' ); | |
| if ( ! class_exists( 'VZR_Updater_Helper' ) ) { | |
| class VZR_Updater_Helper { | |
| /** | |
| * Helper: Add values as options to ACF | |
| * | |
| * For example, to create the fields: | |
| * | |
| * VZR_ACF_Helper::create_group_field( 'Group 1', 'field_group_name', array( | |
| * 'sub_fields' => array( | |
| * VZR_ACF_Helper::create_subgroup_field( 'Field 1', 'field_name_1', 'text' ), | |
| * VZR_ACF_Helper::create_subgroup_field( 'Subgroup 1', 'field_group_inside_group_name_1', 'group', array( | |
| * 'sub_fields' => array( | |
| * VZR_ACF_Helper::create_subgroup_field( 'Option 2', 'field_name_2', 'text' ), | |
| * VZR_ACF_Helper::create_subgroup_field( 'Option 3', 'field_name_3', 'text' ), | |
| * ), | |
| * ) ), | |
| * ), | |
| * ) ), | |
| * | |
| * You should pass: | |
| * $this->add_acf_option( 'field_group_name', array( | |
| * 'field_name_1' => 'Value of option', | |
| * 'field_group_inside_group_name_1' => array( | |
| * 'field_name_2' => 'Value of option 2', | |
| * 'field_name_3' => 'Value of option 3', | |
| * ), | |
| * ) | |
| * | |
| * @param string $field_name The key used on VZR_ACF_Helper's create method | |
| * @param string|array $value The value for option or a array for group field (with the fields itself) | |
| * @param string $subgrop_name Used internally to keep option name when field is inside a group | |
| */ | |
| public function add_acf_option( $field_name, $value, $subgrop_name = '' ) { | |
| if ( is_array( $value ) ) { | |
| foreach ( $value as $sub_field_name => $sub_field_value ) { | |
| $group_option = ( $subgrop_name ) ? $subgrop_name : $field_name; | |
| $group_option .= '_' . $sub_field_name; | |
| $this->add_acf_option( $sub_field_name, $sub_field_value, $group_option ); | |
| } | |
| return; | |
| } | |
| $option = 'options_'; | |
| $option .= ( $subgrop_name ) ? $subgrop_name : $field_name; | |
| update_option( $option, $value ); | |
| update_option( '_' . $option, $field_name ); | |
| } | |
| /** | |
| * Helper: Add a page/post trying to keep it unique by slug/post_type | |
| * | |
| * @param array $post_arr @see wp_insert_post() | |
| */ | |
| public function add_unique_page( $post_arr ) { | |
| if ( empty( $post_arr['post_title'] ) ) return false; | |
| if ( empty( $post_arr['post_content'] ) ) { | |
| $post_arr['post_content'] = ''; | |
| } | |
| if ( empty( $post_arr['post_name'] ) ) { | |
| $post_arr['post_name'] = sanitize_title( $post_arr['post_title'] ); | |
| } | |
| if ( empty( $post_arr['post_status'] ) ) { | |
| $post_arr['post_status'] = 'publish'; | |
| } | |
| // Check page exists | |
| $args = array( | |
| 'name' => $post_arr['post_name'], | |
| 'post_status' => 'any', | |
| ); | |
| if ( ! empty( $post_arr['post_type'] ) ) { | |
| $args['post_type'] = $post_arr['post_type']; | |
| } | |
| $current_page = ( get_posts( $args )[0] ) ?? false; | |
| if ( $current_page && $current_page->post_name == $post_arr['post_name'] ) { | |
| $post_arr['ID'] = $current_page->ID; | |
| } | |
| return wp_insert_post( $post_arr ); | |
| } | |
| /** | |
| * Helper: Add a Elementor page/post trying to keep it unique by slug/post_type | |
| * | |
| * @param array $post_arr @see wp_insert_post() | |
| * @param array $elementor_data Array of elementor widgets data to save to database | |
| */ | |
| public function add_unique_elementor_page( $post_arr, $elementor_data = array() ) { | |
| if ( empty( $post_arr['post_type'] ) ) $post_arr['post_type'] = 'page'; | |
| $page_id = $this->add_unique_page( $post_arr ); | |
| if ( ! $page_id || ! is_plugin_active( 'elementor/elementor.php' ) ) return $page_id; | |
| // Getting template | |
| $page_template = $post_arr['page_template'] ?? get_post_meta( $page_id, '_wp_page_template', true ); | |
| $page_template = ( empty( $page_template ) ) ? 'default' : $page_template; | |
| // Elementor | |
| $elementor = \Elementor\Plugin::instance(); | |
| // Save elementor data | |
| $elementor->db->set_is_elementor_page( $page_id ); | |
| $document = $elementor->documents->get( $page_id ); | |
| $document->save( array( | |
| 'elements' => $elementor_data, | |
| 'settings' => array( | |
| 'template' => $page_template, | |
| 'post_status' => $post_arr['post_status'] ?? 'publish', | |
| ), | |
| ) ); | |
| // Remove Post CSS to recreate | |
| delete_post_meta( $page_id, '_elementor_css' ); | |
| return $page_id; | |
| } | |
| /** | |
| * Helper: Add a Elementor local template trying to keep it unique by slug/post_type | |
| * | |
| * @param array $post_arr @see wp_insert_post() | |
| * @param array $elementor_data Array of elementor widgets data to save to database | |
| * @param string $template_type Template type: section | widget | page | |
| */ | |
| public function add_unique_elementor_template( $post_arr, $elementor_data = array(), $template_type = 'section' ) { | |
| $post_arr['post_type'] = 'elementor_library'; | |
| $page_id = $this->add_unique_elementor_page( $post_arr, $elementor_data ); | |
| if ( ! $page_id ) return $page_id; | |
| update_post_meta( $page_id, '_elementor_template_type', $template_type ); | |
| wp_set_object_terms( $page_id, $template_type, 'elementor_library_type' ); | |
| } | |
| /** | |
| * Helper: Update the option to all sites | |
| * | |
| * @see update_option() | |
| */ | |
| public function update_all_options( $option, $new_value, $autoload = null ) { | |
| $sites = get_sites(); | |
| foreach ( $sites as $site ) { | |
| switch_to_blog( $site->blog_id ); | |
| update_option( $option, $new_value, $autoload ); | |
| restore_current_blog(); | |
| } | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment