| [21] | 1 | <?php |
|---|
| 2 | /* |
|---|
| 3 | |
|---|
| 4 | ___Canvas Import Functions______________________________________ |
|---|
| 5 | |
|---|
| 6 | Imports function name and variable definitions from an XML file. |
|---|
| 7 | |
|---|
| 8 | ________________________________________________________________ |
|---|
| 9 | |
|---|
| 10 | */ |
|---|
| 11 | |
|---|
| 12 | |
|---|
| 13 | function canvas_delete_block($block_id) { |
|---|
| 14 | global $wpdb, $canvas; |
|---|
| 15 | $wpdb->query("DELETE FROM ".$canvas->main." WHERE block_id = '$block_id'"); |
|---|
| 16 | if($variables = $wpdb->get_results("SELECT variable_id, type FROM ".$canvas->variables." WHERE parent = '$block_id'")) { |
|---|
| 17 | foreach($variables as $variable) { |
|---|
| 18 | $wpdb->query("DELETE FROM ".$canvas->variables." WHERE parent = '$block_id'"); |
|---|
| 19 | if($variable->type == 'Radio' || $variable->type == 'Image' || $variable->type == 'Select') |
|---|
| 20 | $wpdb->query("DELETE FROM ".$canvas->options." WHERE var_id = '".$variable->variable_id."'"); |
|---|
| 21 | } |
|---|
| 22 | } |
|---|
| 23 | } |
|---|
| 24 | |
|---|
| 25 | function canvas_delete_block_by_mod_ID($customWriteModuleId) { |
|---|
| 26 | global $wpdb, $canvas; |
|---|
| 27 | |
|---|
| 28 | if ($blocks = $wpdb->get_results("SELECT block_id FROM ".$canvas->main." WHERE module_id = ".$customWriteModuleId)){ |
|---|
| 29 | foreach($blocks as $block) |
|---|
| 30 | canvas_delete_block($block->block_id); |
|---|
| 31 | } |
|---|
| 32 | } |
|---|
| 33 | |
|---|
| 34 | |
|---|
| 35 | |
|---|
| 36 | |
|---|
| 37 | function canvas_import_form() { |
|---|
| 38 | ?> |
|---|
| 39 | <div class="lbContent"> |
|---|
| 40 | <h3>Import from XML |
|---|
| 41 | <p>If you're using a correctly built Canvas-compatible theme, you can import layout schemes from other users.</p> |
|---|
| 42 | <a id="cancel" href="javascript:void(0)" class="lbAction" rel="cancel">Cancel</a> |
|---|
| 43 | </h3> |
|---|
| 44 | <div> |
|---|
| 45 | <form id="canvas_export"> |
|---|
| 46 | <p>Canvas can load layout and Canvas plugin data through a special XML file. To import a layout, be sure to use an XML file for the theme that you are currently working with.</p> |
|---|
| 47 | <p>Importing from XML is still experimental, and may result in loss of Canvas-related data.</p> |
|---|
| 48 | <label class="textbox">Path to the XML file on your server:<input class="text" type="text" id="path_to_xml" name="path_to_xml" value="" /></label> |
|---|
| 49 | <input type="button" id="import_canvas" name="import_canvas" value="Import" onclick="Canvas.importXml()"/> |
|---|
| 50 | </form> |
|---|
| 51 | <p id="export_message"></p> |
|---|
| 52 | </div> |
|---|
| 53 | </div> |
|---|
| 54 | |
|---|
| 55 | <?php |
|---|
| 56 | } |
|---|
| 57 | |
|---|
| 58 | function canvas_import_layout($filename) { |
|---|
| 59 | if(!file_exists($filename)) { echo 'File does not exist.'; return; } |
|---|
| 60 | global $wpdb, $canvas, $imported_theme, $imported_blocks, $imported_variables, $id, $current_tag; |
|---|
| 61 | $imported_theme = array(); |
|---|
| 62 | $imported_blocks = array(); |
|---|
| 63 | canvas_reload(); |
|---|
| 64 | |
|---|
| 65 | if(!($xmlparser = xml_parser_create())) |
|---|
| 66 | $error = "ERROR: Cannot create parser for ".$filename; |
|---|
| 67 | xml_set_element_handler($xmlparser, "startElement", "endElement"); |
|---|
| 68 | xml_set_character_data_handler($xmlparser, "canvas_import_layout_XML"); |
|---|
| 69 | |
|---|
| 70 | if(!($fp = fopen($filename, "r"))) |
|---|
| 71 | $error = "ERROR: Cannot open ".$filename; |
|---|
| 72 | while($data = fread($fp, 4096)) { |
|---|
| 73 | $data = eregi_replace(">"."[[:space:]]+"."<","><",$data); |
|---|
| 74 | $data = eregi_replace(">"."[[:space:]]+",">",$data); |
|---|
| 75 | $data = eregi_replace("[[:space:]]+"."<","<",$data); |
|---|
| 76 | |
|---|
| 77 | if (!xml_parse($xmlparser, $data, feof($fp))) |
|---|
| 78 | $error = "XML error parsing ".$filename; |
|---|
| 79 | } |
|---|
| 80 | xml_parser_free($xmlparser); |
|---|
| 81 | |
|---|
| 82 | $block_ids = $wpdb->get_results("SELECT block_id FROM ".$canvas->main." WHERE theme = '".get_option('template')."'"); |
|---|
| 83 | foreach($block_ids as $block_id) { |
|---|
| 84 | canvas_delete_block($block_id->block_id); |
|---|
| 85 | } |
|---|
| 86 | canvas_install(); // clean install |
|---|
| 87 | |
|---|
| 88 | foreach($imported_blocks as $key => $block) { |
|---|
| 89 | $old_block = $wpdb->get_results("SELECT block_id, zone FROM ".$canvas->main." WHERE ubi = '".$block['ubi']."'"); |
|---|
| 90 | $old_block = $old_block[0]; |
|---|
| 91 | //if($old_block->zone != 'shelf') $old_block = canvas_duplicate_block($old_block->block_id); |
|---|
| 92 | |
|---|
| 93 | $wpdb->query("UPDATE ".$canvas->main." SET zone = '".$block['zone']."', position = '".$block['position']."' WHERE block_id = '".$old_block->block_id."'"); |
|---|
| 94 | if($imported_variables && $block['type'] == 'plugin') { |
|---|
| 95 | foreach($imported_variables[$key] as $variables) { |
|---|
| 96 | $wpdb->query("UPDATE ".$canvas->variables." SET value = '".addslashes($variables['value'])."' WHERE variable_name = '".addslashes($variables['name'])."' AND parent = '".$old_block->block_id."'"); |
|---|
| 97 | } |
|---|
| 98 | } |
|---|
| 99 | } |
|---|
| 100 | } |
|---|
| 101 | |
|---|
| 102 | function canvas_import_plugin($moduleID, $filename, $position, $modTitle, $template_page, $duplicate_id, $block_id = -1) { |
|---|
| 103 | |
|---|
| 104 | global $wpdb, $canvas; |
|---|
| 105 | if(substr($filename, -13) == 'configure.xml') { |
|---|
| 106 | $plugin = canvas_get_plugin($filename, $modTitle); |
|---|
| 107 | $path = $plugin->functionPath; |
|---|
| 108 | list($plugin_id, $position) = canvas_install_plugin_data($moduleID, $plugin, $position, $template_page, $duplicate_id, $block_id); |
|---|
| 109 | if (!empty($plugin->variables)) { |
|---|
| 110 | |
|---|
| 111 | // Import or update variables |
|---|
| 112 | foreach($plugin->variables as $variable) { |
|---|
| 113 | //$name = $path.'_'.str_replace(array(' ','.'), '_', addslashes($variable['name'])); |
|---|
| 114 | $name = str_replace(array(' ','.'), '_', addslashes($variable['name'])); |
|---|
| 115 | $type = $variable['type']; |
|---|
| 116 | $default = addslashes($variable['default']); |
|---|
| 117 | $description = addslashes($variable['description']); |
|---|
| 118 | $options = $variable['option']; |
|---|
| 119 | canvas_install_variable_data($name, $plugin_id, $type, $default, $description, $options); |
|---|
| 120 | } |
|---|
| 121 | } |
|---|
| 122 | // Delete obselete variables |
|---|
| 123 | $blockVars = $wpdb->get_results("SELECT * FROM ".$canvas->variables." WHERE parent = '$plugin_id'"); |
|---|
| 124 | foreach($blockVars as $blockVar){ |
|---|
| 125 | $found = false; |
|---|
| 126 | if (!empty($plugin->variables)){ |
|---|
| 127 | foreach($plugin->variables as $variable) { |
|---|
| 128 | //$name = $path.'_'.str_replace(array(' ','.'), '_', addslashes($variable['name'])); |
|---|
| 129 | $name = str_replace(array(' ','.'), '_', addslashes($variable['name'])); |
|---|
| 130 | if ($name == $blockVar->variable_name) $found = true; |
|---|
| 131 | } |
|---|
| 132 | } |
|---|
| 133 | if (!$found){ |
|---|
| 134 | $wpdb->query("DELETE FROM ".$canvas->variables." WHERE variable_id = ". $blockVar->variable_id); |
|---|
| 135 | } |
|---|
| 136 | } |
|---|
| 137 | |
|---|
| 138 | } |
|---|
| 139 | return $plugin; |
|---|
| 140 | } |
|---|
| 141 | |
|---|
| 142 | |
|---|
| 143 | function get_template_pages($template) { |
|---|
| 144 | $result = array(); |
|---|
| 145 | $filename = ABSPATH.'/wp-content/themes/'.$template.'/canvas.php'; |
|---|
| 146 | |
|---|
| 147 | if(file_exists($filename)) { |
|---|
| 148 | $file_data = implode('', file($filename)); |
|---|
| 149 | preg_match_all("|<!-- Canvas Page:(\s*)(.*) -->|", $file_data, $pages); |
|---|
| 150 | |
|---|
| 151 | if(!empty($pages[2])) { |
|---|
| 152 | foreach($pages[2] as $page) { |
|---|
| 153 | list($page_name, $id) = split('\s*\|\s*',$page); |
|---|
| 154 | $result[] = trim($id); |
|---|
| 155 | } |
|---|
| 156 | } |
|---|
| 157 | } |
|---|
| 158 | |
|---|
| 159 | return $result; |
|---|
| 160 | } |
|---|
| 161 | |
|---|
| 162 | function canvas_import_plugins($position, $dir='') { |
|---|
| 163 | global $wpdb, $canvas; |
|---|
| 164 | |
|---|
| 165 | include_once "RCCWP_Application.php"; |
|---|
| 166 | $customModules = RCCWP_CustomWriteModule::GetCustomModules(); |
|---|
| 167 | $currDuplicates = $wpdb->get_results("SELECT * FROM ".$canvas->duplicates); |
|---|
| 168 | |
|---|
| 169 | $template = get_option('template'); |
|---|
| 170 | $template_pages = get_template_pages($template); |
|---|
| 171 | |
|---|
| 172 | |
|---|
| 173 | |
|---|
| 174 | $currModules = $wpdb->get_results("SELECT * FROM ".$canvas->main); |
|---|
| 175 | foreach($currModules as $currModule){ |
|---|
| 176 | |
|---|
| 177 | // Delete obselete modules |
|---|
| 178 | $found = false; |
|---|
| 179 | foreach($customModules as $customModule){ |
|---|
| 180 | if ($customModule->id == $currModule->module_id) $found = true; |
|---|
| 181 | } |
|---|
| 182 | if (!$found){ |
|---|
| 183 | canvas_delete_block_by_mod_ID($currModule->module_id); |
|---|
| 184 | $wpdb->query("DELETE FROM ".$canvas->duplicates." WHERE module_id = '$currModule->module_id'"); |
|---|
| 185 | } |
|---|
| 186 | |
|---|
| 187 | // Delete obselete duplicates |
|---|
| 188 | $found = false; |
|---|
| 189 | foreach($currDuplicates as $currDuplicate){ |
|---|
| 190 | if ($currDuplicate->duplicate_id == $currModule->duplicate_id) $found = true; |
|---|
| 191 | } |
|---|
| 192 | if (!$found && $currModule->duplicate_id != 0){ |
|---|
| 193 | canvas_delete_block($currModule->block_id); |
|---|
| 194 | } |
|---|
| 195 | |
|---|
| 196 | |
|---|
| 197 | |
|---|
| 198 | } |
|---|
| 199 | |
|---|
| 200 | |
|---|
| 201 | |
|---|
| 202 | // Insert/Update modules in canvas table |
|---|
| 203 | foreach($customModules as $customModule){ |
|---|
| 204 | |
|---|
| 205 | foreach($template_pages as $template_page){ |
|---|
| 206 | |
|---|
| 207 | $currModules = $wpdb->get_results("SELECT * FROM ".$canvas->main." WHERE module_id = '$customModule->id' AND theme = '".get_option('template')."' AND page='$template_page'"); |
|---|
| 208 | |
|---|
| 209 | |
|---|
| 210 | // --- The module doesn't exist in the database, insert it for the first time |
|---|
| 211 | if (!$currModules){ |
|---|
| 212 | $configFile = $dir.$customModule->name."/configure.xml"; |
|---|
| 213 | if (file_exists($configFile)){ |
|---|
| 214 | $plugin = canvas_import_plugin($customModule->id, $configFile, $position, $customModule->name, $template_page, 0); |
|---|
| 215 | $position++; |
|---|
| 216 | } |
|---|
| 217 | } |
|---|
| 218 | |
|---|
| 219 | // --- If a duplicate doesn't exist, insert it |
|---|
| 220 | $currDuplicates = $wpdb->get_results("SELECT * FROM ".$canvas->duplicates." WHERE module_id = '$customModule->id'"); |
|---|
| 221 | foreach($currDuplicates as $currDuplicate){ |
|---|
| 222 | if (!$wpdb->get_results("SELECT * FROM ".$canvas->main." WHERE module_id = '$customModule->id' AND theme = '".get_option('template')."' AND page='$template_page' AND duplicate_id = $currDuplicate->duplicate_id")){ |
|---|
| 223 | $configFile = $dir.$customModule->name."/configure.xml"; |
|---|
| 224 | if (file_exists($configFile)){ |
|---|
| 225 | $plugin = canvas_import_plugin($customModule->id, $configFile, $position, $currDuplicate->duplicate_name, $template_page, $currDuplicate->duplicate_id); |
|---|
| 226 | $position++; |
|---|
| 227 | } |
|---|
| 228 | } |
|---|
| 229 | } |
|---|
| 230 | |
|---|
| 231 | |
|---|
| 232 | // --- Update each instance of this module |
|---|
| 233 | foreach($currModules as $currModule){ |
|---|
| 234 | $configFile = $dir.$customModule->name."/configure.xml"; |
|---|
| 235 | |
|---|
| 236 | // Get module name |
|---|
| 237 | include_once('RCCWP_CustomWriteModule.php'); |
|---|
| 238 | $customWriteModule = RCCWP_CustomWriteModule::Get($currModule->module_id); |
|---|
| 239 | $currModule->module_name = $customWriteModule->name; |
|---|
| 240 | |
|---|
| 241 | $modID = $currModule->block_id; |
|---|
| 242 | $modName = $currModule->module_name; |
|---|
| 243 | $modTemplateName = $currModule->template_name; // $wpdb->get_var("SELECT template_name FROM ".$canvas->main." WHERE module_name = '$customModule->name' AND theme = '".get_option('template')."'"); |
|---|
| 244 | $modTemplateSize = $currModule->template_size; //$wpdb->get_var("SELECT template_size FROM ".$canvas->main." WHERE module_name = '$customModule->name' AND theme = '".get_option('template')."'"); |
|---|
| 245 | |
|---|
| 246 | // Check whether there is a custom configure.xml defined for this module |
|---|
| 247 | $overidConfig = get_module_template_folder($modName, $modTemplateName, $modTemplateSize)."configure.xml"; |
|---|
| 248 | |
|---|
| 249 | if (file_exists($overidConfig)) |
|---|
| 250 | $configFile = $overidConfig; |
|---|
| 251 | |
|---|
| 252 | if (file_exists($configFile)){ |
|---|
| 253 | $plugin = canvas_import_plugin($customModule->id, $configFile, $position, $modName, $template_page, $currDuplicate->duplicate_id, $modID); |
|---|
| 254 | $position++; |
|---|
| 255 | } |
|---|
| 256 | } |
|---|
| 257 | |
|---|
| 258 | } |
|---|
| 259 | } |
|---|
| 260 | |
|---|
| 261 | |
|---|
| 262 | return array($plugin, $position); |
|---|
| 263 | } |
|---|
| 264 | |
|---|
| 265 | |
|---|
| 266 | function canvas_install_plugin_data($moduleID, $plugin, $position, $template_page, $duplicate_id, $block_id = -1) { |
|---|
| 267 | global $wpdb, $canvas; |
|---|
| 268 | //$name = addslashes($plugin->functionName); |
|---|
| 269 | $author = $plugin->functionAuthor; |
|---|
| 270 | $uri = $plugin->functionUri; |
|---|
| 271 | $description = addslashes($plugin->functionDesc); |
|---|
| 272 | $group = $plugin->functionGroup; |
|---|
| 273 | $path = $plugin->functionPath; |
|---|
| 274 | $ubi = md5($name.$author.$uri.$description.$group.$path); |
|---|
| 275 | if ($block_id == -1){ |
|---|
| 276 | //if (!$wpdb->get_var("SELECT module_id FROM ".$canvas->main." WHERE module_id = $moduleID AND theme = '".get_option('template')."' AND page='$template_page'") ) { |
|---|
| 277 | // install module for the first time |
|---|
| 278 | $wpdb->query("INSERT INTO ".$canvas->main." (module_id, type, zone, position, author, description, block_group, uri, path, theme, ubi, duplicate_id, page) VALUES ($moduleID, 'plugin','shelf','$position','$author','$description','$group','$uri','$path','".get_option('template')."','$ubi', $duplicate_id, '$template_page')"); |
|---|
| 279 | $position++; |
|---|
| 280 | $plugin_id = mysql_insert_id($wpdb->dbh); |
|---|
| 281 | } |
|---|
| 282 | else{ |
|---|
| 283 | |
|---|
| 284 | //if ($wpdb->get_var("SELECT block_id FROM ".$canvas->main." WHERE block_id = $block_id")) |
|---|
| 285 | //If this the main module, not a duplicate, update its name |
|---|
| 286 | $wpdb->query("UPDATE ".$canvas->main. |
|---|
| 287 | " SET author = '$author', description = '$description', path ='$path'". |
|---|
| 288 | " WHERE block_id = $block_id"); |
|---|
| 289 | //..else |
|---|
| 290 | // $wpdb->query("UPDATE ".$canvas->main. |
|---|
| 291 | // " SET author = '$author', description = '$description', path ='$path'". |
|---|
| 292 | // " WHERE block_id = $block_id"); |
|---|
| 293 | |
|---|
| 294 | $plugin_id = $wpdb->get_var("SELECT block_id FROM ".$canvas->main." WHERE block_id = $block_id"); |
|---|
| 295 | $position = $wpdb->get_var("SELECT position FROM ".$canvas->main." WHERE block_id = $block_id"); |
|---|
| 296 | } |
|---|
| 297 | |
|---|
| 298 | |
|---|
| 299 | return array($plugin_id,$position); |
|---|
| 300 | } |
|---|
| 301 | |
|---|
| 302 | function canvas_install_variable_data($name, $parent, $type, $default, $description, $options) { |
|---|
| 303 | global $wpdb, $canvas; |
|---|
| 304 | |
|---|
| 305 | if (!$wpdb->get_var("SELECT variable_name FROM ".$canvas->variables." WHERE variable_name = '$name' AND parent = '$parent'") ) { |
|---|
| 306 | $wpdb->query("INSERT INTO ".$canvas->variables." (variable_name,parent,type,value,default_value,description) VALUES ('$name','$parent','$type','$default','$default','$description')"); |
|---|
| 307 | $var_id = $wpdb->get_var("SELECT variable_id FROM ".$canvas->variables." WHERE variable_name = '$name' AND parent = '$parent'"); |
|---|
| 308 | |
|---|
| 309 | if (!empty($options)) { |
|---|
| 310 | foreach($options as $option) { |
|---|
| 311 | $option_name = addslashes($option['name']); |
|---|
| 312 | $option_value = addslashes($option['value']); |
|---|
| 313 | $option_params = addslashes($option['params']); |
|---|
| 314 | $wpdb->query("INSERT INTO ".$canvas->options." (var_id, option_text, option_value, option_params) VALUES ('$var_id','$option_name','$option_value','$option_params')"); |
|---|
| 315 | } |
|---|
| 316 | } |
|---|
| 317 | } |
|---|
| 318 | else{ |
|---|
| 319 | $extraParam = ''; |
|---|
| 320 | if ('__RESETDEFAULT__' == $wpdb->get_var("SELECT default_value FROM ".$canvas->variables." WHERE variable_name = '$name' AND parent = '$parent'")) |
|---|
| 321 | $extraParam = ", value = '$default' "; |
|---|
| 322 | |
|---|
| 323 | $wpdb->query("UPDATE ".$canvas->variables. |
|---|
| 324 | " SET type = '$type', default_value = '$default', description = '$description' ". $extraParam. |
|---|
| 325 | " WHERE variable_name = '$name' AND parent = '$parent'"); |
|---|
| 326 | $var_id = $wpdb->get_var("SELECT variable_id FROM ".$canvas->variables." WHERE variable_name = '$name' AND parent = '$parent'"); |
|---|
| 327 | $wpdb->query("DELETE FROM ".$canvas->options." WHERE var_id = '$var_id'"); |
|---|
| 328 | |
|---|
| 329 | if (!empty($options)) { |
|---|
| 330 | foreach($options as $option) { |
|---|
| 331 | $option_name = addslashes($option['name']); |
|---|
| 332 | $option_value = addslashes($option['value']); |
|---|
| 333 | $option_params = addslashes($option['params']); |
|---|
| 334 | $wpdb->query("INSERT INTO ".$canvas->options." (var_id,option_text,option_value, option_params) VALUES ('$var_id','$option_name','$option_value','$option_params')"); |
|---|
| 335 | } |
|---|
| 336 | } |
|---|
| 337 | } |
|---|
| 338 | } |
|---|
| 339 | |
|---|
| 340 | |
|---|
| 341 | |
|---|
| 342 | function canvas_restore_form($xml_path) { |
|---|
| 343 | ?> |
|---|
| 344 | <div class="lbContent"> |
|---|
| 345 | <h3>Restore from XML |
|---|
| 346 | <p>Attempt to restore this theme's layout to the default settings.</p> |
|---|
| 347 | <a id="cancel" href="javascript:void(0)" class="lbAction" rel="cancel">Cancel</a> |
|---|
| 348 | </h3> |
|---|
| 349 | <div> |
|---|
| 350 | <form id="canvas_export"> |
|---|
| 351 | <p>Canvas will attempt to restore your current theme's layout and plugin settings to those specified by the author in the original XML file.</p> |
|---|
| 352 | <p>Importing from XML is still experimental, and may result in loss of Canvas-related data.</p> |
|---|
| 353 | <input class="text" type="hidden" id="path_to_xml" name="path_to_xml" value="<?php echo $xml_path; ?>&restore=true" /> |
|---|
| 354 | <input type="button" id="import_canvas" name="import_canvas" value="Restore" onclick="Canvas.importXml()"/> |
|---|
| 355 | </form> |
|---|
| 356 | <p id="export_message"></p> |
|---|
| 357 | </div> |
|---|
| 358 | </div> |
|---|
| 359 | |
|---|
| 360 | <?php |
|---|
| 361 | } |
|---|
| 362 | |
|---|
| 363 | ?> |
|---|