• 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 7 years, 1 month ago by ralbusta.
Viewing 10 replies - 31 through 40 (of 40 total)
  • Gulshan Kumar

    (@thegulshankumar)

    @tufty Good to hear from you about alternative option. You are 100% right, it’s works for only those plugin which is available at www.ads-software.com such as Code Snippets.

    My site is in localhost and I have white screen. I dont know what to do ?? Please help me…

    • This reply was modified 7 years, 1 month ago by ggulyesil.
    • This reply was modified 7 years, 1 month ago by ggulyesil.
    anonymized-14293447

    (@anonymized-14293447)

    @ggulyesil and everyone else.
    In stead of trying all these hacks, that can cause even more damage you might not notice until later, I suggest you pull out your backup and restore it, like I did. Then leave Snippets in stand by for a week or so. Eventually delete it.

    (@ggulyesil) Just rename the code-snippets folder in the plugins folder – that’s all you need to do ??

    Gulshan Kumar

    (@thegulshankumar)

    If installed locally on localhost: Use Exporer or Finder (Mac) to find the directory tree of your wp install. Go to ../wp-content/plugins/. Delete the folder “code-snippets”. After that you’ll be able to login again. Check the plugins list, remove Code Snippets if it’s still there. Then install the plugin as usual.

    • This reply was modified 7 years, 1 month ago by Anton_Th.
    Plugin Author Shea Bunge

    (@bungeshea)

    Yep, on localhost is even easier. Just delete the wp-content/plugins/code-snippets directory using your file manager, then reinstall Code Snippets through the WordPress plugins menu.

    @thegulshankumar Exactly what I did. That method solved issues with many plugins for me in the past. And WP Rollback is a handy tool to have somewhere near your plugins directory (move it there and activate only when needed).

    Gulshan Kumar

    (@thegulshankumar)

    @bungeshea

    I am bit confused, which one is best? In my case below both methods fixed the issue

    1. Rename plugin, rolling to previous version.

    At other site, I tried this way
    2. Rename plugin, deleted plugin, exported all code in JSON, installed Code Snippet once again and imported JSON data. That means, here latest version worked.

    So, it’s a question. What method should I go?

    Server environment: PHP 7.2

    Thanks & Regards,
    Gulshan

    (@thegulshankumar) – the problem has been fixed for some time now – if your site is still crippled due to the plugin, just rename the existing folder, then the site will work and then re-install.

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