static function _create_file($src, $des) { if ($src == $des) { return $src; } $read_file = LP_PLUGIN_PATH . '/' . $src; $write_file = LP_PLUGIN_PATH . '/' . $des; global $wp_filesystem; if ($wp_filesystem) { include_once LP_PLUGIN_PATH . '/inc/libraries/minify/src/Minify.php'; include_once LP_PLUGIN_PATH . '/inc/libraries/minify/src/JS.php'; $code = $wp_filesystem->get_contents($read_file); $minifier = new JS($code); $code = $minifier->minify(); $wp_filesystem->put_contents($write_file, $code); if (file_exists($write_file)) { $src = $des; } } return $src; }
public function buildTheViewScript($viewArray) { // set the view name $viewName = ComponentbuilderHelper::safeString($viewArray['settings']->name_single); // add conditions to this view if (isset($viewArray['settings']->conditions) && ComponentbuilderHelper::checkArray($viewArray['settings']->conditions)) { // reset defaults $getValue = array(); $ifValue = array(); $targetControls = array(); $functions = array(); foreach ($viewArray['settings']->conditions as $condition) { if (isset($condition['match_name']) && ComponentbuilderHelper::checkString($condition['match_name'])) { $uniqueVar = $this->uniquekey(7); $matchName = $condition['match_name'] . '_' . $uniqueVar; $targetBehavior = $condition['target_behavior'] == 1 ? 'show' : 'hide'; $targetDefault = $condition['target_behavior'] == 1 ? 'hide' : 'show'; // make sure only one relation is set $firstTime = true; // set the realtation if any if ($condition['target_relation'] && $firstTime) { // chain to other items of the same target $relations = $this->getTargetRelationScript($viewArray['settings']->conditions, $condition, $viewName); if (ComponentbuilderHelper::checkArray($relations)) { // set behavior and default array $behaviors[$matchName] = $targetBehavior; $defaults[$matchName] = $targetDefault; // set the type buket $typeBuket[$matchName] = $condition['match_type']; // set function array $functions[$uniqueVar][0] = $matchName; $matchNames[$matchName] = $condition['match_name']; // get the select value $getValue[$matchName] = $this->getValueScript($condition['match_type'], $condition['match_name'], $condition['match_extends'], $uniqueVar); // get the options $options = $this->getOptionsScript($condition['match_type'], $condition['match_options']); // set the if values $ifValue[$matchName] = $this->ifValueScript($matchName, $condition['match_behavior'], $condition['match_type'], $options); // set the target controls $targetControls[$matchName] = $this->setTargetControlsScript($condition['target_field'], $targetBehavior, $targetDefault, $uniqueVar, $viewName); $firstTime = false; foreach ($relations as $relation) { if (ComponentbuilderHelper::checkString($relation['match_name'])) { $relationName = $relation['match_name'] . '_' . $uniqueVar; // set the type buket $typeBuket[$relationName] = $relation['match_type']; // set function array $functions[$uniqueVar][] = $relationName; $matchNames[$relationName] = $relation['match_name']; // get the relation option $relationOptions = $this->getOptionsScript($relation['match_type'], $relation['match_options']); $getValue[$relationName] = $this->getValueScript($relation['match_type'], $relation['match_name'], $condition['match_extends'], $uniqueVar); $ifValue[$relationName] = $this->ifValueScript($relationName, $relation['match_behavior'], $relation['match_type'], $relationOptions); } } } } else { // set behavior and default array $behaviors[$matchName] = $targetBehavior; $defaults[$matchName] = $targetDefault; // set the type buket $typeBuket[$matchName] = $condition['match_type']; // set function array $functions[$uniqueVar][0] = $matchName; $matchNames[$matchName] = $condition['match_name']; // get the select value $getValue[$matchName] = $this->getValueScript($condition['match_type'], $condition['match_name'], $condition['match_extends'], $uniqueVar); // get the options $options = $this->getOptionsScript($condition['match_type'], $condition['match_options']); // set the if values $ifValue[$matchName] = $this->ifValueScript($matchName, $condition['match_behavior'], $condition['match_type'], $options); // set the target controls $targetControls[$matchName] = $this->setTargetControlsScript($condition['target_field'], $targetBehavior, $targetDefault, $uniqueVar, $viewName); } } } // reset buckets $initial = ''; $func = ''; $validation = ''; $isSet = ''; $listener = ''; if (ComponentbuilderHelper::checkArray($functions)) { // now build the initial script $initial .= "// Initial Script\njQuery(document).ready(function()"; $initial .= "\n{"; foreach ($functions as $function => $matchKeys) { $func_call = $this->buildFunctionCall($function, $matchKeys, $getValue); $initial .= $func_call['code']; } $initial .= "});\n"; // for modal fields $modal = ''; // now build the listener scripts foreach ($functions as $l_function => $l_matchKeys) { $funcCall = ''; foreach ($l_matchKeys as $l_matchKey) { $name = $matchNames[$l_matchKey]; $matchTypeKey = $typeBuket[$l_matchKey]; $funcCall = $this->buildFunctionCall($l_function, $l_matchKeys, $getValue); if (isset($this->setScriptMediaSwitch) && ComponentbuilderHelper::checkArray($this->setScriptMediaSwitch) && in_array($matchTypeKey, $this->setScriptMediaSwitch)) { $modal .= $funcCall['code']; } else { if (isset($this->setScriptUserSwitch) && ComponentbuilderHelper::checkArray($this->setScriptUserSwitch) && in_array($matchTypeKey, $this->setScriptUserSwitch)) { $name = $name . '_id'; } $listener .= "\n// #jform_" . $name . " listeners for " . $l_matchKey . " function"; $listener .= "\njQuery('#jform_" . $name . "').on('keyup',function()"; $listener .= "\n{"; $listener .= $funcCall['code']; $listener .= "\n});"; $listener .= "\njQuery('#adminForm').on('change', '#jform_" . $name . "',function (e)"; $listener .= "\n{"; $listener .= "\n\te.preventDefault();"; $listener .= $funcCall['code']; $listener .= "\n});\n"; } } } if (ComponentbuilderHelper::checkString($modal)) { $listener .= "\nwindow.SqueezeBox.initialize({"; $listener .= "\n\tonClose:function(){"; $listener .= $modal; $listener .= "\n\t}"; $listener .= "\n});\n"; } // now build the function $func = ''; $head = ''; foreach ($functions as $f_function => $f_matchKeys) { $map = ''; // does this function require an array $addArray = false; $func_ = $this->buildFunctionCall($f_function, $f_matchKeys, $getValue); // set array switch if ($func_['array']) { $addArray = true; } $func .= "\n// the " . $f_function . " function"; $func .= "\nfunction " . $f_function . "("; $fucounter = 0; foreach ($f_matchKeys as $fu_matchKey) { if (ComponentbuilderHelper::checkString($fu_matchKey)) { if ($fucounter == 0) { $func .= $fu_matchKey; } else { $func .= ',' . $fu_matchKey; } $fucounter++; } } $func .= ")"; $func .= "\n{"; if ($addArray) { foreach ($f_matchKeys as $a_matchKey) { $name = $matchNames[$a_matchKey]; $func .= "\n\tif (isSet(" . $a_matchKey . ") && " . $a_matchKey . ".constructor !== Array)\n\t{\n\t\tvar temp_" . $f_function . " = " . $a_matchKey . ";\n\t\tvar " . $a_matchKey . " = [];\n\t\t" . $a_matchKey . ".push(temp_" . $f_function . ");\n\t}"; $func .= "\n\telse if (!isSet(" . $a_matchKey . "))\n\t{"; $func .= "\n\t\tvar " . $a_matchKey . " = [];"; $func .= "\n\t}"; $func .= "\n\tvar " . $name . " = " . $a_matchKey . ".some(" . $a_matchKey . "_SomeFunc);\n"; // setup the map function $map .= "\n// the " . $f_function . " Some function"; $map .= "\nfunction " . $a_matchKey . "_SomeFunc(" . $a_matchKey . ")"; $map .= "\n{"; $map .= "\n\t//" . $this->setLine(__LINE__) . " set the function logic"; $map .= "\n\tif ("; $if = $ifValue[$a_matchKey]; if (ComponentbuilderHelper::checkString($if)) { $map .= $if; } $map .= ")"; $map .= "\n\t{"; $map .= "\n\t\treturn true;"; $map .= "\n\t}\n\treturn false;"; $map .= "\n}\n"; } $func .= "\n\n\t//" . $this->setLine(__LINE__) . " set this function logic"; $func .= "\n\tif ("; // set if counter $aifcounter = 0; foreach ($f_matchKeys as $af_matchKey) { $name = $matchNames[$af_matchKey]; if ($aifcounter == 0) { $func .= $name; } else { $func .= ' && ' . $name; } $aifcounter++; } $func .= ")\n\t{"; } else { $func .= "\n\t//" . $this->setLine(__LINE__) . " set the function logic"; $func .= "\n\tif ("; // set if counter $ifcounter = 0; foreach ($f_matchKeys as $f_matchKey) { $if = $ifValue[$f_matchKey]; if (ComponentbuilderHelper::checkString($if)) { if ($ifcounter == 0) { $func .= $if; } else { $func .= ' && ' . $if; } $ifcounter++; } } $func .= ")\n\t{"; } // get the controles $controls = $targetControls[$f_matchKeys[0]]; // get target behavior and default $targetBehavior = $behaviors[$f_matchKeys[0]]; $targetDefault = $defaults[$f_matchKeys[0]]; // load the target behavior foreach ($controls as $target => $action) { $func .= $action['behavior']; if (ComponentbuilderHelper::checkString($action['hide'])) { $func .= $action[$targetBehavior]; $head .= $action['requiredVar']; } } $func .= "\n\t}\n\telse\n\t{"; foreach ($controls as $target => $action) { $func .= $action['default']; if (ComponentbuilderHelper::checkString($action['hide'])) { $func .= $action[$targetDefault]; } } $func .= "\n\t}\n}\n" . $map; } // add the needed validation to file if (isset($this->validationFixBuilder[$viewName]) && ComponentbuilderHelper::checkArray($this->validationFixBuilder[$viewName])) { $validation .= "\n// update required fields"; $validation .= "\nfunction updateFieldRequired(name,status)"; $validation .= "\n{"; $validation .= "\n\tvar not_required = jQuery('#jform_not_required').val();"; $validation .= "\n\n\tif(status == 1)"; $validation .= "\n\t{"; $validation .= "\n\t\tif (isSet(not_required) && not_required != 0)"; $validation .= "\n\t\t{"; $validation .= "\n\t\t\tnot_required = not_required+','+name;"; $validation .= "\n\t\t}"; $validation .= "\n\t\telse"; $validation .= "\n\t\t{"; $validation .= "\n\t\t\tnot_required = ','+name;"; $validation .= "\n\t\t}"; $validation .= "\n\t}"; $validation .= "\n\telse"; $validation .= "\n\t{"; $validation .= "\n\t\tif (isSet(not_required) && not_required != 0)"; $validation .= "\n\t\t{"; $validation .= "\n\t\t\tnot_required = not_required.replace(','+name,'');"; $validation .= "\n\t\t}"; $validation .= "\n\t}"; $validation .= "\n\n\tjQuery('#jform_not_required').val(not_required);"; $validation .= "\n}\n"; } // set the isSet function $isSet = "\n// the isSet function"; $isSet .= "\nfunction isSet(val)"; $isSet .= "\n{"; $isSet .= "\n\tif ((val != undefined) && (val != null) && 0 !== val.length){"; $isSet .= "\n\t\treturn true;"; $isSet .= "\n\t}"; $isSet .= "\n\treturn false;"; $isSet .= "\n}"; } // load to this buket $fileScript = $initial . $func . $validation . $isSet; $footerScript = $listener; } // add custom script to file if (isset($this->customScriptBuilder['view_file'][$viewName]) && ComponentbuilderHelper::checkString($this->customScriptBuilder['view_file'][$viewName])) { if (!isset($fileScript)) { $fileScript = ''; } $fileScript .= "\n\n" . str_replace(array_keys($this->placeholders), array_values($this->placeholders), $this->customScriptBuilder['view_file'][$viewName]); } // add custom script to footer if (isset($this->customScriptBuilder['view_footer'][$viewName]) && ComponentbuilderHelper::checkString($this->customScriptBuilder['view_footer'][$viewName])) { $customFooterScript = "\n\n" . str_replace(array_keys($this->placeholders), array_values($this->placeholders), $this->customScriptBuilder['view_footer'][$viewName]); if (strpos($customFooterScript, '<?php') === false) { // only add now if no php is added to the footer script if (!isset($footerScript)) { $footerScript = ''; } $footerScript .= $customFooterScript; unset($customFooterScript); } } // minfy the script if ($this->params->get('minify') && isset($fileScript) && ComponentbuilderHelper::checkString($fileScript)) { // minify the fielScript javscript $minifier = new JS(); $minifier->add($fileScript); $fileScript = $minifier->minify(); } // minfy the script if ($this->params->get('minify') && isset($footerScript) && ComponentbuilderHelper::checkString($footerScript)) { // minify the footerScript javscript $minifier = new JS(); $minifier->add($footerScript); $footerScript = $minifier->minify(); } // make sure there is script to add if (isset($fileScript) && ComponentbuilderHelper::checkString($fileScript)) { // add the head script if set if (isset($head) && ComponentbuilderHelper::checkString($head)) { $fileScript = "// Some Global Values\n" . $head . "\n" . $fileScript; } // load the script $this->editBodyViewScriptBuilder[$viewName]['fileScript'] = $fileScript; } // make sure to add custom footer script if php was found in it, since we canot minfy it with php if (isset($customFooterScript) && ComponentbuilderHelper::checkString($customFooterScript)) { if (!isset($footerScript)) { $footerScript = ''; } $footerScript .= $customFooterScript; } // make sure there is script to add if (isset($footerScript) && ComponentbuilderHelper::checkString($footerScript)) { // add the needed script tags $footerScript = "\n\n" . '<script type="text/javascript">' . "\n" . $footerScript . "\n</script>"; $this->editBodyViewScriptBuilder[$viewName]['footerScript'] = $footerScript; } }