• Resolved ralbusta

    (@ralbusta)


    Use this file to fix the broken update:

    <?php
    /**
    * This file includes functions for importing and exporting snippets
    */
    /**
    *
    * @access private
    *
    * @param $snippets
    * @param null $multisite
    * @param string $dup_action
    *
    * @return array
    */
    function _code_snippets_save_imported_snippets( $snippets, $multisite = null, $dup_action = ‘ignore’ ) {
    /* Get a list of existing snippet names keyed to their IDs */
    $existing_snippets = array();
    if ( ‘replace’ == $dup_action || ‘skip’ === $dup_action ) {
    $all_snippets = get_snippets( array(), $multisite );
    foreach ( $all_snippets as $snippet ) {
    if ( $snippet->name ) {
    $existing_snippets[ $snippet->name ] = $snippet->id;
    }
    }
    }
    /* Save a record of the snippets which were imported */
    $imported = array();
    /* Loop through the provided snippets */
    foreach ( $snippets as $snippet ) {
    /* Check if the snippet already exists */
    if ( ‘ignore’ !== $dup_action && isset( $existing_snippets[ $snippet->name ] ) ) {
    /* If so, either overwrite the existing ID, or skip this import */
    if ( ‘replace’ === $dup_action ) {
    $snippet->id = $existing_snippets[ $snippet->name ];
    } elseif ( ‘skip’ === $dup_action ) {
    continue;
    }
    }
    /* Save the snippet and increase the counter if successful */
    if ( $snippet_id = save_snippet( $snippet ) ) {
    $imported[] = $snippet_id;
    }
    }
    return $imported;
    }
    /**
    * Imports snippets from a JSON file
    *
    * @since 2.9.7
    *
    * @uses save_snippet() to add the snippets to the database
    *
    * @param string $file The path to the file to import
    * @param bool|null $multisite Import into network-wide table or site-wide table?
    * @param string $dup_action Action to take if duplicate snippets are detected. Can be ‘skip’, ‘ignore’, or ‘replace’
    *
    * @return array|bool An array of imported snippet IDs on success, false on failure
    */
    function import_snippets_json( $file, $multisite = null, $dup_action = ‘ignore’ ) {
    if ( ! file_exists( $file ) || ! is_file( $file ) ) {
    return false;
    }
    $raw_data = file_get_contents( $file );
    $data = json_decode( $raw_data, true );
    $snippets = array();
    /* Reformat the data into snippet objects */
    foreach ( $data[‘snippets’] as $snippet ) {
    $snippet = new Code_Snippet( $snippet );
    $snippet->network = $multisite;
    $snippets[] = $snippet;
    }
    $imported = _code_snippets_save_imported_snippets( $snippets, $multisite, $dup_action );
    do_action( ‘code_snippets/import/json’, $file, $multisite );
    return $imported;
    }
    /**
    * Imports snippets from an XML file
    *
    * @since 2.0
    *
    * @uses save_snippet() to add the snippets to the database
    *
    * @param string $file The path to the file to import
    * @param bool|null $multisite Import into network-wide table or site-wide table?
    * @param string $dup_action Action to take if duplicate snippets are detected. Can be ‘skip’, ‘ignore’, or ‘replace’
    *
    * @return array|bool An array of imported snippet IDs on success, false on failure
    */
    function import_snippets_xml( $file, $multisite = null, $dup_action = ‘ignore’ ) {
    if ( ! file_exists( $file ) || ! is_file( $file ) ) {
    return false;
    }
    $dom = new DOMDocument( ‘1.0’, get_bloginfo( ‘charset’ ) );
    $dom->load( $file );
    $snippets_xml = $dom->getElementsByTagName( ‘snippet’ );
    $fields = array( ‘name’, ‘description’, ‘desc’, ‘code’, ‘tags’, ‘scope’ );
    $snippets = array();
    /* Loop through all snippets */
    /** @var DOMElement $snippet_xml */
    foreach ( $snippets_xml as $snippet_xml ) {
    $snippet = new Code_Snippet();
    $snippet->network = $multisite;
    /* Build a snippet object by looping through the field names */
    foreach ( $fields as $field_name ) {
    /* Fetch the field element from the document */
    $field = $snippet_xml->getElementsByTagName( $field_name )->item( 0 );
    /* If the field element exists, add it to the snippet object */
    if ( isset( $field->nodeValue ) ) {
    $snippet->set_field( $field_name, $field->nodeValue );
    }
    }
    /* Get scope from attribute */
    $scope = $snippet_xml->getAttribute( ‘scope’ );
    if ( ! empty( $scope ) ) {
    $snippet->scope = $scope;
    }
    $snippets[] = $snippet;
    }
    $imported = _code_snippets_save_imported_snippets( $snippets, $dup_action, $multisite );
    do_action( ‘code_snippets/import/xml’, $file, $multisite );
    return $imported;
    }
    /**
    * Set up the current page to act like a downloadable file instead of being shown in the browser
    *
    * @param string $format
    * @param array $ids
    * @param string $table_name
    * @param string $mime_type
    *
    * @return array
    */
    function code_snippets_prepare_export( $format, $ids, $table_name = ”, $mime_type = ” ) {
    global $wpdb;
    /* Fetch the snippets from the database */
    if ( ” === $table_name ) {
    $table_name = code_snippets()->db->get_table_name();
    }
    if ( count( $ids ) ) {
    $sql = sprintf(
    ‘SELECT * FROM %s WHERE id IN (%s)’, $table_name,
    implode( ‘,’, array_fill( 0, count( $ids ), ‘%d’ ) )
    );
    $snippets = $wpdb->get_results( $wpdb->prepare( $sql, $ids ), ARRAY_A );
    } else {
    $snippets = array();
    }
    /* Build the export filename */
    if ( 1 == count( $ids ) ) {
    /* If there is only snippet to export, use its name instead of the site name */
    $first_snippet = new Code_Snippet( $snippets[0] );
    $title = strtolower( $first_snippet->name );
    } else {
    /* Otherwise, use the site name as set in Settings > General */
    $title = strtolower( get_bloginfo( ‘name’ ) );
    }
    $filename = “{$title}.code-snippets.{$format}”;
    $filename = apply_filters( ‘code_snippets/export/filename’, $filename, $title );
    /* Set HTTP headers */
    header( ‘Content-Disposition: attachment; filename=’ . sanitize_file_name( $filename ) );
    if ( ” !== $mime_type ) {
    header( “Content-Type: $mime_type; charset=” . get_bloginfo( ‘charset’ ) );
    }
    /* Return the retrieved snippets to build the rest of the export file */
    return $snippets;
    }
    /**
    * Export snippets to a downloadable PHP file
    *
    * @param $ids
    * @param $table_name
    */
    function download_snippets( $ids, $table_name = ” ) {
    $snippets = code_snippets_prepare_export( ‘php’, $ids, $table_name );
    echo “<?php\n”;
    /* Loop through the snippets */
    foreach ( $snippets as $snippet ) {
    $snippet = new Code_Snippet( $snippet );
    echo “\n/**\n * {$snippet->name}\n”;
    if ( ! empty( $snippet->desc ) ) {
    /* Convert description to PhpDoc */
    $desc = strip_tags( str_replace( “\n”, “\n * “, $snippet->desc ) );
    echo ” *\n * $desc\n”;
    }
    echo ” */\n{$snippet->code}\n”;
    }
    exit;
    }
    /**
    * Export snippets in JSON format
    *
    * @param array $ids list of snippet IDs to export
    * @param string $table_name name of the database table to fetch snippets from
    */
    function export_snippets( $ids, $table_name = ” ) {
    $raw_snippets = code_snippets_prepare_export( ‘json’, $ids, $table_name, ‘application/json’ );
    $final_snippets = array();
    foreach ( $raw_snippets as $snippet ) {
    $snippet = new Code_Snippet( $snippet );
    $fields = array( ‘name’, ‘desc’, ‘tags’, ‘scope’, ‘code’ );
    $final_snippet = array();
    foreach ( $fields as $field ) {
    if ( ! empty( $snippet->$field ) ) {
    $final_snippet[ $field ] = $snippet->$field;
    }
    }
    if ( $final_snippet ) {
    $final_snippets[] = $final_snippet;
    }
    }
    $data = array(
    ‘generator’ => ‘Code Snippets v’ . CODE_SNIPPETS_VERSION,
    ‘date_created’ => date( ‘Y-m-d H:i’ ),
    ‘snippets’ => $final_snippets,
    );
    echo wp_json_encode( $data, apply_filters( ‘code_snippets/export/json_encode_options’, 0 ) );
    exit;
    }

    Save as import-export.php in the php folder of code-snippets plugin. They forgot to add the file to the update

    Don’t Copy/Paste as this is causing problems for some. Use download link in my other post.

    • This topic was modified 6 years, 9 months ago by ralbusta.
Viewing 15 replies - 1 through 15 (of 40 total)
  • Thread Starter ralbusta

    (@ralbusta)

    https://www.dropbox.com/s/6g3xipjarcxmpap/import-export.php?dl=0

    Here’s the file for easy ftp uploading.

    Pretty disappointing that 5+ hours after this was reported and started bringing many sites down…. the developer is nowhere to be seen. That gives me pause for thought about this plugin moving forward. Stuff happens, but rolling out an update should coincide with the developer being available.

    anonymized-14293447

    (@anonymized-14293447)

    oh you can be sure that’s the END for me with this piece of shit

    To be fair, I’ve never had any issues with this in the past. That said, there’s a responsibility that comes with pushing out an update – even a free one – and I really would have liked to see the developer at LEAST acknowledge the issue and that it is being investigated (although, from my limited scanning of this forum, the fix seems pretty simple).

    Thread Starter ralbusta

    (@ralbusta)

    Pretty disappointing that I had to find the source code to fix my site…

    ralbusta thanks – many thanks for diagnosing and sharing the solution

    The developer is in Tasmania, Australia
    Time there is currently 6:45 am
    Not excusing the issue, but it looks like the update was late Saturday night his time.

    Good to know. Pushing out an update and then going to bed is probably also not a great idea. S/he is going to have an interesting Sunday morning ??

    Unfortunately, I am getting the following error message when using the fix above

    Parse error: syntax error, unexpected ‘.0’ (T_DNUMBER), expecting ‘,’ or ‘)’ in /****/****/public_html/wp-content/plugins/code-snippets/php/import-export.php on line 93

    Any ideas why this might be?

    EdFoC (@edfoc)

    I’m just renaming my code snippets folders (file manager in cPanel, or FTP) – easiest solution for now until fixed!

    @edfoc I updated 8 sites, no problem. I downloaded the file (don’t copy/paste the source, that can give errors) and dragged it or FTP’d it to the folder “wp-content\plugins\code-snippets\php

    anonymized-14293447

    (@anonymized-14293447)

    myself, I even had other plugins affected by this. I disable one, the site is back, I re-enable it and it breaks telling me that there’s one more plugin with problems. I do the same, and there I go with a third plugin affected. I’m down in the gutter.

    Thread Starter ralbusta

    (@ralbusta)

    Don’t know. It is working fine for me. Did you copy the text above or download the file?

    anonymized-14293447

    (@anonymized-14293447)

    yep, I copied that file and at least the website came back, from the CodeSnippet point of view (though I have it disabled, otherwise it break everything again), but other plugins are affected.

    @arsenalemusica that’s very very weird. I can only imagine that you had code snippets that were needed by other plugins somehow. Just place the file from @ralbusta in the right folder and this problem is gone and all affected functionality should be back.

Viewing 15 replies - 1 through 15 (of 40 total)
  • The topic ‘Fix new update’ is closed to new replies.