awesomemotive / one-click-demo-import

One Click Demo Import plugin
https://www.awesomemotive.com/
195 stars 83 forks source link

Modifying the Plugin #209

Closed Pixelous closed 3 years ago

Pixelous commented 4 years ago

Hi,

I have added font uploading option to cusomizer and would like fonts imported to the new server. I would like to add some functions into inc/CustomizerImporter.php. I assume if the author have not added hooks there nothing can be done to override the plugin, only to make a copy of it and adding my changes there, correct?

Kind regards

capuderg commented 4 years ago

Hi @Pixelous,

could you please first let us know what kind of hooks you would need to be able to use our plugin and then your custom code for font imports?

We could maybe add some filters/actions to the plugin if there is a need for it, but we can't make any promises. If we agree on a set of hooks, it would be really great if you could prepare a Pull Request for it. This would speed things up greatly.

Pixelous commented 4 years ago

Hi @capuderg

Sorry for the late reply.

Ok, I have these functions that is similar for importing images that the plugin is already have:

/**
     * Imports fonts for settings saved as mods.
 *
 * @since 0.1
 * @access private
 * @param array $mods An array of customizer mods.
 * @return array The mods array with any new import data.
 */
static private function _import_fonts( $mods ) {

    foreach ( $mods as $f => $val ) {

        if ( is_string( $val ) ) {
            if ( strpos( $f, '_typography_font' ) !== false ) {
                $val = preg_split( '/(,)(?=(?:[^}]|{[^{]*})*$)/', $val );
                foreach ( $val as $font ) {
                    $font = html_entity_decode( $font );
                    $font = json_decode( $font, true );
                    $font['src'] = explode( ',', $font['src'] );
                    foreach ( $font['src'] as $key => $val ) {

                        if ( self::_is_font_url( $val ) ) {

                            if ( is_string( $val ) ) {
                                $data = self::_sideload_font( $val );
                                if ( ! is_wp_error( $data ) ) {
                                    if ( isset( $data->url ) ) {
                                        $val = $data->url;
                                        $font['src'][$key] = $val;
                                    }
                                }
                            }

                        }

                    }
                    $font['src'] = implode( ',', $font['src'] );
                    $val = json_encode( $font, JSON_UNESCAPED_SLASHES );
                }
                $mods[$f] = $val;
            }
        }

    }

    return $mods;
}

/**
 * Taken from the core media_sideload_image function and
 * modified to return an array of data instead of html.
 *
 * @since 0.1
 * @access private
 * @param string $file The image file path.
 * @return array An array of image data.
 */
static private function _sideload_font( $file ) {
    $data = new stdClass();

    if ( ! function_exists( 'media_handle_sideload' ) ) {
        require_once( ABSPATH . 'wp-admin/includes/media.php' );
        require_once( ABSPATH . 'wp-admin/includes/file.php' );
        require_once( ABSPATH . 'wp-admin/includes/image.php' );
    }
    if ( ! empty( $file ) ) {

        // Set variables for storage, fix file filename for query strings.
        preg_match( '/[^\?]+\.(woff|woff2)\b/i', $file, $matches );
        if ( isset( $matches[0] ) ) {
            $file_array = array();
            $file_array['name'] = basename( $matches[0] );

            // Download file to temp location.
            $file_array['tmp_name'] = download_url( $file );

            // If error storing temporarily, return the error.
            if ( is_wp_error( $file_array['tmp_name'] ) ) {
                return $file_array['tmp_name'];
            }

            // Do the validation and storage stuff.
            $id = media_handle_sideload( $file_array, 0 );

            // If error storing permanently, unlink.
            if ( is_wp_error( $id ) ) {
                @unlink( $file_array['tmp_name'] );
                return $id;
            }

            // Build the object to return.
            $meta                   = wp_get_attachment_metadata( $id );
            $data->attachment_id    = $id;
            $data->url              = wp_get_attachment_url( $id );
        }
    }

    return $data;
}

/**
 * Checks to see whether a string is an font url or not.
 *
 * @since 0.1
 * @access private
 * @param string $string The string to check.
 * @return bool Whether the string is an font url or not.
 */
static private function _is_font_url( $string = '' ) {
    if ( is_string( $string ) ) {

        if ( preg_match( '/\.(woff|woff2)/i', $string ) ) {
            return true;
        }
    }

    return false;
}

The plugin has this line of code:

$data['mods'] = self::_import_images( $data['mods'] );

And I would like to add after it my line for importing fonts:

$data['mods'] = self::_import_fonts( $data['mods'] );

I assume adding two do_action() functions in inc/CustomizerImporter.php would help?

Kind regards

capuderg commented 4 years ago

Hi,

could you maybe use the pt-ocdi/customizer_import_execution hook? if you want to import the fonts after the customizer import is run, you can use add_action( 'pt-ocdi/customizer_import_execution', 'your_custom_function_name_callback_goes_here' ), 11, 1 ); -> priority 11 will make sure your code executes after the customizer import.

In this callback function, you can run your code with fonts and then loop over the "mods" and update the post meta, like so:

// Loop through the mods and save the mods.
foreach ( $data['mods'] as $key => $val ) {
    set_theme_mod( $key, $val );
}

Take care!

Pixelous commented 4 years ago

Hi @capuderg sorry for late reply. Testing it and it works fine except my fonts importing twice somehow.

I deleted my function to download fonts and then fonts downloading only once but the urls are old then, not from the downloaded fonts.

Any hints?

Kind regards

Pixelous commented 4 years ago

Ok I understand what is going on. Fonts import twice because the plugin uses default import and then I use my own function to import the fonts.

The problem is if I remove my own function how to update font urls in my customizer fields then? Is it possible to fetch imported fonts urls?

capuderg commented 4 years ago

Hi,

where do the fonts get imported the first time? From your customizer import file? Could you export a new/fresh customizer file and resolve the issue?

Otherwise, you would have to overwrite the font URLs after the import. There are probably WP functions that can retrieve and then save the customizer options... I'm not familiar with the customizer API, but there has to be a way to do so.

Take care!

Pixelous commented 4 years ago

@capuderg I have simply removed the fonts import data from the xml file, looks works fine. Will use it as it is for now.

Otherwise the fonts importing twice. The first import is xml file and the second one is my custom function.

I did notice the default build in logo and favicon options import correctly. I have took a look there is a script at the bottom of the customizer page:

var _wpCustomizeSettings =

And logo and favicon options have IDs there.

Take a look at

site_icon

"attachment":{"id":1127,"title":"cropped-favicon.png","filename":"cropped-favicon.png"

So as you can see favicon importing with a help of attachement id.

Just thoughts.

Kind regards

capuderg commented 3 years ago

Hi,

I'll close this GH issue as it looks like the existing hooks were enough to solve this issue.

Take care!