i am trying to make a plugin through Worpress, i have in the frontend a list of generated plugins
like so Plugins
what i am trying to do is to get these plugins to be stored into an array inside the class and be printed or processed inside any function or a callback if i clicked submit. and here is the code.
class Example {
private static $init_core_plugins = array();
private static function handle_post() {
$save_settings = filter_input( INPUT_POST, 'save_settings' );
if ( !is_null( $save_settings ) && check_admin_referer( 'save-settings' ) ) {
self::save_settings();
self::refresh_page();
}
}
private static function save_settings() {
$core_plugins = filter_input( INPUT_POST, 'coreplugins', FILTER_DEFAULT, FILTER_REQUIRE_ARRAY );
if ( empty( $core_plugins ) ) {
$core_plugins = [];
}
self::$init_core_plugins = array_merge( $core_plugins, self::$init_core_plugins );
// it shows here that it holds the correct selected plugins
echo "<pre>";
var_dump (self::$init_core_plugins);
echo "</pre>";
update_option( 'core_plugins_settings', $core_plugins );
}
public static function admin_render_main_page(){
// it shows here that it is an empty array
echo "<pre>";
var_dump (self::$init_core_plugins);
echo "</pre>";
}
}
What i am facing is somehow simple problem but i can't figure out where is the problem, when i called the array inside var_dump() inside save_settings() it printed the selected plugins from the list normally, but the problem is when later i try to var_dump (self::$init_core_plugins) in any other function like in the admin_render_main_page(), it returns as empty array, and the passed data through the save_settings() doesn't seem to be stored in the array.
Related
I am hoping I can get some assistance with some issues I'm trying to get the array data from one function to be used in another function.
So in the function that's creating the array data, it looks like this:
function wlp_generate_pdf_and_save( $data, $post_id ){
// Function needing array data
}
function wlp_generate_preview($data, $generateType){
$out_product_brands = array();
if( count($all_posts) > 0 ){
foreach( $all_posts as $s_post )
{
...
$out_product_brands[] = array( 'brand' => vooHelperNew::get_posts_products( $s_post->ID ) );
...
}
}
}
This function "wlp_generate_preview" works perfectly, but it's this $out_product_brands data i need to use in the function (wlp_generate_pdf_and_save) above this one.
Does the order the functions are place change anything?
Return the data from wlp_generate_preview then pass it as a parameter of wlp_generate_pdf_and_save
First you want to return the array from your first function and then send that as a variable to the second function:
<?php
function wlp_generate_pdf_and_save( $data, $post_id, $input_array ){ //added a third
varible to this functions input
// Function needing array data
}
function wlp_generate_preview($data, $generateType){
$out_product_brands = array();
if( count($all_posts) > 0 ){
foreach( $all_posts as $s_post )
{
...
$out_product_brands[] = array( 'brand' => vooHelperNew::get_posts_products( $s_post->ID ) );
...
}
}
return $out_product_brands;//Returned The array
}
And then when calling your functions you might do something like this:
$input_array = wlp_generate_preview($data, $generateType);//Calls the first function
wlp_generate_pdf_and_save($data, $post_id, $input_array);//The third variable passes it into the next function
I use a method in my class that returns an array of members:
public function getArrTranslatable()
{
//------------------------- DECLARE ---------------------------//
return array( $this->artDescription, //
$this->artKeywords, //
$this->artMaintenanceMsg, //
$this->artTitle );
}
In another class, i get that array then iterate on it to perform some actions, like instanciate array for each members:
$arrFieldToTranslate = $p_translatable->getArrTranslatable();
//------------------------- DECLARE ---------------------------//
foreach( $arrFieldToTranslate as $artField )
{
// Instanciate translatable array.
if( $artField == null )
{
$artField = array();
}
$artField[ $p_idLang ] = "";
}
The code seems to work fine, because i pass in the array instanciation, but when i get thoses members in my view, later, they're null.
Im working on a register function that will register users into the database.
I want a checker in that function that checks if any of the arguments are empty. I've simplified the problem, so this is not the retail look.
<?php
create_user($_POST["username"], $_POST["epost"]);
function create_user($username, $epost){
// Pull all the arguments in create_user and check if they are empty
// Instead of doing this:
if(empty($username) || empty($epost)){
}
}
Reason for making this is so i can simply add another argument to the function and it checks automatically that it isnt empty.
Shorted question:
How do I check if all the arguments in a function isnt empty?
function create_user($username, $epost){
foreach(func_get_args() as $arg)
{
//.. check the arg
}
}
You can use array_filter and array_map functions also.
For example create a function like below
<?php
function isEmpty( $items, $length ) {
$items = array_map( "trim", $items );
$items = array_filter( $items );
return ( count( $items ) !== (int)$length );
}
?>
the above function accepts two parameters.
$items = array of arguments,
$length = the number of arguments the function accepts.
you can use it like below
<?php
create_user( $_POST["username"], $_POST["epost"] );
function create_user( $username, $epost ) {
if ( isEmpty( func_get_args(), 2 ) ) {
// some arguments are empty
}
}
?>
I used this solution for i18n of my CakePHP 2.3 website.
When user in this URL: example.com/myController/myAction/param1/param2
I want to give link to example.com/eng/myController/myAction/param1/param2
This works well with this code inside my view file:
English
But when user in this URL: example.com/fre/myController/myAction/param1/param2
I can't link him to this URL: example.com/eng/myController/myAction/param1/param2
I can get the full URL with this:
fullURL = Router::url( $this->here, true );
$strippedURL = str_replace("http://example.com/fre/myController/myAction/param1/param2","myController/myAction/param1/param2",$fullURL)
But I need to make this for every language. Or I can strip first 22 characters from $fullURL. But they don't seem good solutions.
Do you have any suggestions?
Edit: Inside my Helper/View file I used this:
function getRelURL() {
$controller = $this->request->params['controller'];
$action = $this->request->params['action'];
$URL = "/".$controller."/".$action."/";
foreach ($this->request->params['pass'] as $p) {
$URL .= urlencode($p)."/";
}
}
I would be happy if you can recommend better alternative.
I used a slightly different approach that seems to work well, even though I have not tested it with named parameters and all other routing features.
I created the following function in AppHelper:
public function urlLanguage($lang) {
static $hereUrl=null;
if (empty($hereUrl)) {
$hereUrl = $this->request->params;
unset ( $hereUrl ['models'] );
unset ( $hereUrl ['named'] );
unset ( $hereUrl ['paging'] );
if (isset ( $hereUrl ['pass'] )) {
foreach ( $hereUrl ['pass'] as $pass ) {
$hereUrl [] = $pass;
}
unset ( $hereUrl ['pass'] );
}
}
return array_merge( $hereUrl, array ('language' => $lang ) );
}
and use it in the View as follows:
foreach ($languages as $lang=>$langDesc) {
echo $this->Html->link($languageDesc, $this->Html->urlLanguage($lang));
}
where $languages is an array with all the available language. I skipped the HTML around it the link itself.
I followed the instructions here for setting up the routes and language switching.
I have a controller something like this in codeigniter:
class ABC extends MX_Controller {
...
...
...
.
.
.
...
...
public function getTestPostData(){
print_r($_POST);
}
}
In getTestPostData when i print $_POST i need full $_POST data without any sanitization of any kind. I can't set $config['global_xss_filtering'] = FALSE; in config.php
How can i achieve that?
Edit:
In post data i am sending some HTML content with some <script>(script) tags, but when i print the data in my controller using $this->input->post("content") the <script>(script)tags gets replace by [removed] tags. This happens when the global_xss_filtering is TRUE. I can't change this value to FALSE.
I hope now the question is clear.
Try this, in my tests it worked even after unsetting $_POST completely.
public function getTestPostData()
{
$post_data = explode( "&", file_get_contents('php://input') );
$result = array();
foreach( $post_data as $post_datum ) {
$pair = explode("=", $post_datum );
$result[urldecode($pair[0])] = urldecode($pair[1]);
}
print_r( $result );
}
you can't set $config['global_xss_filtering'] = FALSE; in config.php?
then if you can't access core/input.php class
and remove _POST from it
function _sanitize_globals()
{
// It would be "wrong" to unset any of these GLOBALS.
$protected = array('_SERVER', '_GET', '_POST', '_FILES', '_REQUEST',
'_SESSION', '_ENV', 'GLOBALS',
or change the var $xss_clean to false like this
function get_post($index = '', $xss_clean = FALSE)
{
if ( ! isset($_POST[$index]) )
{
return $this->get($index, false); //here
}
then you can't view the post data unfiltered from
print_r($_POST);
print_r( $this->input->post() )
that's all
If you have CI v. 2+ try the following:
Create a new Core file that extends CI_Security and overwrite the xss_clean function like so:
class MY_Security extends CI_Security {
public function xss_clean($str, $is_image = FALSE)
{
return $str;
}
}
EDIT: I just tested this and it works. FYI.