Beispiel #1
0
 public static function import($url)
 {
     $upload_dir = wp_upload_dir();
     $upload_url = $upload_dir['url'];
     $upload_path = $upload_dir['path'];
     $dirname = pathinfo($url, PATHINFO_FILENAME);
     // filename without extension
     $basename = pathinfo($url, PATHINFO_BASENAME);
     // file with extension
     // Download file
     if (file_put_contents($upload_path . '/' . $basename, file_get_contents($url)) === FALSE) {
         die('Error : can\'t upload.');
     }
     // Check file type
     if (!zipIsValid($upload_path . '/' . $basename)) {
         die('Wrong file.');
     }
     // Extract archive
     $zip = new ZipArchive();
     $res = $zip->open($upload_path . '/' . $basename);
     // new dirname based on real directory on the zip
     $dirname = dirname(trim($zip->getNameIndex(0), '/'));
     if ($dirname == '.') {
         // In some cases (when zip is not generated via export),
         // the getNameIndex already returns the perfect dirname
         // so applying dirname() returns "."
         $dirname = trim($zip->getNameIndex(0), '/');
     }
     if ($res === TRUE) {
         $zip->extractTo($upload_path);
         $zip->close();
     } else {
         die('Error : can\'t upload.');
     }
     // Get the json file
     $exportJson = file_get_contents($upload_path . '/' . $dirname . '/export.json');
     if ($exportJson === FALSE) {
         die('Error : can\'t upload.');
     }
     // Parse raw to json
     $exportJson = json_decode($exportJson, true);
     if ($exportJson == NULL) {
         die('Wrong file.');
     }
     // Import Quiz
     if (!isset($exportJson['settings']['type'])) {
         die('Wrong file.');
     }
     $quizType = $exportJson['settings']['type'];
     $quiz = new $quizType();
     $quiz->add($exportJson['settings']);
     // Upload pictures
     foreach (glob("{$upload_path}/{$dirname}/*.{jpg,png,gif}", GLOB_BRACE) as $key => $file) {
         $basename = pathinfo($file, PATHINFO_BASENAME);
         $oldId = pathinfo($file, PATHINFO_FILENAME);
         $newId = custom_media_sideload_image("{$upload_url}/{$dirname}/{$basename}");
         array_replace_only_for_key($exportJson['content'], 'pictureId', $oldId, $newId);
     }
     // Appreciations
     foreach ($exportJson['appreciations'] as $appreciation) {
         $matches = array();
         preg_match_all('#%%export(.*)%%#', $appreciation['content'], $matches);
         foreach ($matches[1] as $match) {
             $newId = custom_media_sideload_image("{$upload_url}/{$dirname}/{$match}");
             $url = wp_get_attachment_url($newId);
             $appreciation['content'] = str_replace('%%export' . $match . '%%', $url, $appreciation['content']);
         }
         $newAppr = $quiz->addAppreciation($appreciation);
         $oldId = $appreciation['id'];
         $newId = $newAppr->getId();
         array_replace_only_for_key($exportJson['content'], 'appreciationId', $oldId, $newId);
     }
     // Questions + answers
     foreach ($exportJson['content'] as $content) {
         $question = $content['question'];
         $question = $quiz->addQuestion($question);
         // Answers
         foreach ($content['answers'] as $index => $answer) {
             $multipliers = $quizType == 'WPVQGameTrueFalse' ? array() : $answer['multipliers'];
             $question->addAnswer($answer, NULL, $multipliers);
         }
     }
     // ExtraOptions
     if (isset($exportJson['extraOptions'])) {
         foreach ($exportJson['extraOptions'] as $name => $option) {
             self::updateExtraOption($quiz->getId(), $name, $option);
         }
     }
 }
Beispiel #2
0
/**
 * Replace a certain value for a certain key, in a multidimensional array
 */
function array_replace_only_for_key(&$array, $key, $needle, $haystack)
{
    foreach ($array as $k => &$elem) {
        if (is_array($elem)) {
            array_replace_only_for_key($elem, $key, $needle, $haystack);
        } else {
            if ($k == $key && $elem == $needle) {
                $elem = str_replace($elem, $needle, $haystack);
            }
        }
    }
}