示例#1
0
/**
 * 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 XML file to import
 * @param  bool|null  $multisite Import into network-wide table or site-wide table?
 * @return array|bool            An array of imported snippet IDs on success, false on failure
 */
function import_snippets($file, $multisite = null)
{
    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', 'code', 'tags', 'scope');
    $exported_snippets = array();
    /* Loop through all snippets */
    /** @var DOMElement $snippet_xml */
    foreach ($snippets_xml as $snippet_xml) {
        $snippet = new 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->{$field_name} = $field->nodeValue;
            }
        }
        /* Get scope from attribute */
        $scope = $snippet_xml->getAttribute('scope');
        if (!empty($scope)) {
            $snippet->scope = $scope;
        }
        /* Save the snippet and increase the counter if successful */
        if ($snippet_id = save_snippet($snippet)) {
            $exported_snippets[] = $snippet_id;
        }
    }
    do_action('code_snippets/import', $dom, $multisite);
    return $exported_snippets;
}
示例#2
0
 /**
  * Save the posted snippet data to the database and redirect
  */
 private function save_posted_snippet()
 {
     /* Build snippet object from fields with 'snippet_' prefix */
     $snippet = new Snippet();
     foreach ($_POST as $field => $value) {
         if ('snippet_' === substr($field, 0, 8)) {
             /* Remove 'snippet_' prefix from field name */
             $field = substr($field, 8);
             $snippet->{$field} = stripslashes($value);
         }
     }
     /* Activate or deactivate the snippet before saving if we clicked the button */
     // Shared network snippets cannot be network activated
     if (isset($_POST['snippet_sharing']) && 'on' === $_POST['snippet_sharing']) {
         $snippet->active = 0;
         unset($_POST['save_snippet_activate'], $_POST['save_snippet_deactivate']);
     } elseif (isset($_POST['save_snippet_activate'])) {
         $snippet->active = 1;
     } elseif (isset($_POST['save_snippet_deactivate'])) {
         $snippet->active = 0;
     }
     /* Deactivate snippet if code contains errors */
     if ($snippet->active) {
         if ($code_error = $this->validate_code($snippet)) {
             $snippet->active = 0;
         }
     }
     /* Save the snippet to the database */
     $snippet_id = save_snippet($snippet);
     /* Update the shared network snippets if necessary */
     if ($snippet_id && get_current_screen()->in_admin('network')) {
         if (isset($_POST['snippet_sharing']) && 'on' === $_POST['snippet_sharing']) {
             $shared_snippets = get_site_option('shared_network_snippets', array());
             /* Add the snippet ID to the array if it isn't already */
             if (!in_array($snippet_id, $shared_snippets)) {
                 $shared_snippets[] = $snippet_id;
                 update_site_option('shared_network_snippets', array_values($shared_snippets));
             }
         } else {
             $this->unshare_network_snippet($snippet_id);
         }
     }
     /* If the saved snippet ID is invalid, display an error message */
     if (!$snippet_id || $snippet_id < 1) {
         /* An error occurred */
         wp_redirect(add_query_arg('result', 'save-error', code_snippets_get_menu_url('add')));
         exit;
     }
     /* Display message if a parse error occurred */
     if (isset($code_error) && $code_error) {
         wp_redirect(add_query_arg(array('id' => $snippet_id, 'result' => 'code-error'), code_snippets_get_menu_url('edit')));
         exit;
     }
     /* Set the result depending on if the snippet was just added */
     $result = isset($_POST['snippet_id']) ? 'updated' : 'added';
     /* Append a suffix if the snippet was activated or deactivated */
     if (isset($_POST['save_snippet_activate'])) {
         $result .= '-and-activated';
     } elseif (isset($_POST['save_snippet_deactivate'])) {
         $result .= '-and-deactivated';
     }
     /* Redirect to edit snippet page */
     wp_redirect(add_query_arg(array('id' => $snippet_id, 'result' => $result), code_snippets_get_menu_url('edit')));
     exit;
 }
示例#3
0
 /**
  * Save the posted snippet to the database
  * @uses wp_redirect() to pass the results to the page
  * @uses save_snippet() to save the snippet to the database
  */
 private function save_posted_snippet()
 {
     /* Make sure the nonce validates before we do any snippet ops */
     if (!isset($_REQUEST['_wpnonce']) || !wp_verify_nonce($_REQUEST['_wpnonce'], 'save_snippet')) {
         return;
     }
     /* Save the snippet if one has been submitted */
     if (isset($_POST['save_snippet']) || isset($_POST['save_snippet_activate']) || isset($_POST['save_snippet_deactivate'])) {
         /* Build snippet object from fields with 'snippet_' prefix */
         $snippet = new Snippet();
         foreach ($_POST as $field => $value) {
             if ('snippet_' === substr($field, 0, 8)) {
                 /* Remove 'snippet_' prefix from field name */
                 $field = substr($field, 8);
                 $snippet->{$field} = stripslashes($value);
             }
         }
         /* Activate or deactivate the snippet before saving if we clicked the button */
         // Shared network snippets cannot be network activated
         if (isset($_POST['snippet_sharing']) && 'on' === $_POST['snippet_sharing']) {
             $snippet->active = 0;
             unset($_POST['save_snippet_activate'], $_POST['save_snippet_deactivate']);
         } elseif (isset($_POST['save_snippet_activate'])) {
             $snippet->active = 1;
         } elseif (isset($_POST['save_snippet_deactivate'])) {
             $snippet->active = 0;
         }
         /* Save the snippet to the database */
         $snippet_id = save_snippet($snippet);
         /* Update the shared network snippets if necessary */
         if ($snippet_id && get_current_screen()->in_admin('network')) {
             $shared_snippets = get_site_option('shared_network_snippets', array());
             if (isset($_POST['snippet_sharing']) && 'on' === $_POST['snippet_sharing']) {
                 /* Add the snippet ID to the array if it isn't already */
                 if (!in_array($snippet_id, $shared_snippets)) {
                     $shared_snippets[] = $snippet_id;
                     update_site_option('shared_network_snippets', array_values($shared_snippets));
                 }
             } elseif (in_array($snippet_id, $shared_snippets)) {
                 /* Remove the snippet ID from the array */
                 $shared_snippets = array_diff($shared_snippets, array($snippet_id));
                 update_site_option('shared_network_snippets', array_values($shared_snippets));
                 /* Deactivate on all sites */
                 global $wpdb;
                 if ($sites = $wpdb->get_col("SELECT blog_id FROM {$wpdb->blogs}")) {
                     foreach ($sites as $site) {
                         switch_to_blog($site);
                         $active_shared_snippets = get_option('active_shared_network_snippets');
                         if (is_array($active_shared_snippets)) {
                             $active_shared_snippets = array_diff($active_shared_snippets, array($snippet_id));
                             update_option('active_shared_network_snippets', $active_shared_snippets);
                         }
                     }
                     restore_current_blog();
                 }
             }
         }
         /* If the saved snippet ID is invalid, display an error message */
         if (!$snippet_id || $snippet_id < 1) {
             /* An error occurred */
             wp_redirect(add_query_arg('result', 'error', code_snippets_get_menu_url('add')));
             exit;
         }
         /* Set the result depending on if the snippet was just added */
         $result = isset($_POST['snippet_id']) ? 'updated' : 'added';
         /* Append a suffix if the snippet was activated or deactivated */
         if (isset($_POST['save_snippet_activate'])) {
             $result .= '-and-activated';
         } elseif (isset($_POST['save_snippet_deactivate'])) {
             $result .= '-and-deactivated';
         }
         /* Redirect to edit snippet page */
         wp_redirect(add_query_arg(array('id' => $snippet_id, 'result' => $result), code_snippets_get_menu_url('edit')));
         exit;
     } elseif (isset($_POST['snippet_id'], $_POST['delete_snippet'])) {
         delete_snippet($_POST['snippet_id']);
         wp_redirect(add_query_arg('result', 'delete', code_snippets_get_menu_url('manage')));
         exit;
     } elseif (isset($_POST['snippet_id'], $_POST['export_snippet'])) {
         export_snippets($_POST['snippet_id']);
     }
 }
示例#4
0
/**
 * Loads the help tabs for the Edit Snippets page
 *
 * @since 1.0
 * @access private
 * @uses wp_redirect To pass the results to the page
 */
function code_snippets_load_single_menu()
{
    /* Make sure the user has permission to be here */
    if (!current_user_can(get_snippets_cap())) {
        wp_die(__('You are not authorized to access this page.', 'code-snippets'));
    }
    /* Create the snippet tables if they don't exist */
    create_code_snippets_tables();
    /* Load the screen help tabs */
    require plugin_dir_path(__FILE__) . 'admin-help.php';
    /* Enqueue the code editor and other scripts and styles */
    add_action('admin_enqueue_scripts', 'code_snippets_enqueue_codemirror', 9);
    /* Don't allow visiting the edit snippet page without a valid ID */
    if (code_snippets_get_menu_slug('edit') === $_REQUEST['page']) {
        if (!isset($_REQUEST['id']) || 0 == $_REQUEST['id']) {
            wp_redirect(code_snippets_get_menu_url('add'));
            exit;
        }
    }
    /* Make sure the nonce validates before we do any snippet ops */
    if (!isset($_REQUEST['_wpnonce']) || !wp_verify_nonce($_REQUEST['_wpnonce'], 'save_snippet')) {
        return;
    }
    /* Save the snippet if one has been submitted */
    if (isset($_POST['save_snippet']) || isset($_POST['save_snippet_activate']) || isset($_POST['save_snippet_deactivate'])) {
        /* Activate or deactivate the snippet before saving if we clicked the button */
        if (isset($_POST['save_snippet_activate'])) {
            $_POST['snippet_active'] = 1;
        } elseif (isset($_POST['save_snippet_deactivate'])) {
            $_POST['snippet_active'] = 0;
        }
        /* Save the snippet to the database */
        $result = save_snippet(stripslashes_deep($_POST));
        /* Build the status message and redirect */
        $query_args = array();
        if ($result && isset($_POST['save_snippet_activate'])) {
            /* Snippet was activated addition to saving*/
            $query_args['activated'] = true;
        } elseif ($result && isset($_POST['save_snippet_deactivate'])) {
            /* Snippet was deactivated addition to saving*/
            $query_args['deactivated'] = true;
        }
        if (!$result || $result < 1) {
            /* An error occurred */
            $query_args['invalid'] = true;
        } elseif (isset($_POST['snippet_id'])) {
            /* Existing snippet was updated */
            $query_args['id'] = $result;
            $query_args['updated'] = true;
        } else {
            /* New snippet was added */
            $query_args['id'] = $result;
            $query_args['added'] = true;
        }
        /* Redirect to edit snippet page */
        wp_redirect(add_query_arg($query_args, code_snippets_get_menu_url('edit')));
    } elseif (isset($_POST['snippet_id'], $_POST['delete_snippet'])) {
        delete_snippet($_POST['snippet_id']);
        wp_redirect(add_query_arg('delete', true, code_snippets_get_menu_url('manage')));
    } elseif (isset($_POST['snippet_id'], $_POST['export_snippet'])) {
        export_snippets($_POST['snippet_id']);
    }
}
/**
 * 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 XML file to import
 * @param boolean|null $multisite Import into network-wide table or site-wide table?
 * @return integer|boolean The number of snippets imported on success, false on failure
 */
function import_snippets($file, $multisite = null)
{
    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', 'code', 'tags', 'scope');
    $count = 0;
    /* Loop through all snippets */
    foreach ($snippets_xml as $snippet_xml) {
        $snippet = new stdClass();
        /* 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->{$field_name} = $field->nodeValue;
            }
        }
        /* Save the snippet and increase the counter if successful */
        if (save_snippet($snippet, $multisite)) {
            $count += 1;
        }
    }
    do_action('code_snippets/import', $dom, $multisite);
    return $count;
}