Custom Cache per Country - Wordpress php [Disable Cache from function] - php

On our website at www.EllasBubbles.com, we are using a functions.php script to execute an alternative stylesheet to users who are outside of U.S/Canada. This script is as follows:
// [International] - Dark Mode
add_action( 'wp_head' , 'custom_inline_css', 200 );
function custom_inline_css() {
// Get an instance of the WC_Geolocation object class
$geolocation_instance = new WC_Geolocation();
// Get user IP
$user_ip_address = $geolocation_instance->get_ip_address();
// Get geolocated user IP country code.
$user_geolocation = $geolocation_instance->geolocate_ip( $user_ip_address );
// For all countries except 'US'
if($user_geolocation['country'] !== 'US' && $user_geolocation['country'] !== 'CA'){
?>
<style>
<?php include 'international.php'; ?>
</style>
<?php
}
}
We use cloud flare to manage our Cache. I have properly setup bypass rules for these two files:
ellasbubbles.com/wp-content/themes/flatsome/international.php
ellasbubbles.com/wp-content/themes/flatsome/assets/css/flatsome.css
When loading the site from an international VPN, the "Dark Mode" (Outside U.S/Canada) version is displayed. However, when disabling the VPN; we are stuck on Dark Mode. Which is a bypassed file with Cloudflare Cache - and simply makes no sense to me.
This leads me to believe that the instance of the WC_Geolocation class is being cached somewhere on the Wordpress website. I have searched everywhere for a solution, and can not determine the best way to perform this custom CSS per country. Can we disable caching from function custom_inline_css ? Should I change international.php to css? Is there a better way to go about this?
Update 1: When I disabled caching from the entire website in cloud flare, this works fine!
Update 2: When I disable caching from everything inside ellasbubbles.com/wp-content/*; this still does not work properly..what file am I missing?
Update 3: I disabled all folders one by one to no avail
Please help me help everyone

Related

Moodle plugin : check if admin + add link to plugin in administration

I'm new to moodle plugin development and am trying to create a plugin that displays a page to the admin where I can add my on php code.
In brief, what I want the plugin to do I have already achieved in a standard php file that I upload to the moodle root. From here you can call the file e.g. yourdomain.co.uk/moodlelocation/myfile.php and it will run as expected.
The problem with this is it isn't secure since anyone can load the myfile.php and in turn run the scripts on the page. It also means any one else using this script (it will be given away for free when complete) would need to FTP into their hosting and upload two php files to their moodle install.
Due to this, I thought a plugin (a very very basic plugin) may be the best solution. They could then load the page in the admin via the "site administration". e.g. site administration > Development > MyPlugin. I am assuming I could then also restrict the plugin's main page to admins only (??).
So to recap, I can create a php page that has my script all rocking and rolling BUT I need to make this into a plugin.
I did some reading and I think a "local" plugin was the easiest way to go (??).
I have managed to get the local plugin up and running using the below in local/webguides/inex.php :
<?php
// Standard config file and local library.
require_once(__DIR__ . '/../../config.php');
// Setting up the page.
$PAGE->set_context(context_system::instance());
$PAGE->set_pagelayout('standard');
$PAGE->set_title("webguides");
$PAGE->set_heading("webguides");
$PAGE->set_url(new moodle_url('/local/webguides/index.php'));
// Ouput the page header.
echo $OUTPUT->header();
echo 'MY php CODE here etc';
?>
This works fine but with two problems:
Anyone can access it via http://domain/local/webguides/index.php
There is no link to it in the site administration (so the user would need to type the URL in).
Can anyone shed any light how I would achieve the two steps above?
Thanks in advance
p.s. ideally I'd like to keep the plugin to as few files as possible so if the required code could be added to the local/webguides/index.php file it would be preferred.
You need to create a capability, then require that capability before displaying the page.
First, have a look at local/readme.txt - this gives an overview of the files needed for a local plugin.
Or read the documentation at https://docs.moodle.org/dev/Local_plugins
Also have a look at existing local plugins so you can see how they are created - https://moodle.org/plugins/?q=type:local
At a bare minimum, you need
local/webguides/db/access.php - this will have the capability
local/webguides/lang/en/local_webguides.php
local/webguides/version.php
Plus your index file
local/webguides/index.php
In the db/access.php file have something like
defined('MOODLE_INTERNAL') || die();
$capabilities = array(
'local/webguides:view' => array(
'captype' => 'read',
'contextlevel' => CONTEXT_SYSTEM,
'archetypes' => array(
),
),
);
You might also need 'riskbitmask' => RISK_XXX depending on if there are any risks in you code. Such as RISK_CONFIG, RISK_PERSONAL, etc.
In lang/en/local_webguides.php have something like
defined('MOODLE_INTERNAL') || die();
$string['pluginname'] = 'Webguides';
$string['webguides:view'] = 'Able to view webguids';
In version.php have something like
defined('MOODLE_INTERNAL') || die();
$plugin->version = 2020051901; // The current plugin version (Date: YYYYMMDDXX)
$plugin->requires = 2015051109; // Requires this Moodle version.
$plugin->component = 'local_webguides'; // Full name of the plugin (used for diagnostics).
Replace 2015051109 with the version of Moodle you are using - this will be in version.php in the root folder.
Then in your index.php file use this near the top.
require_capability('local/webguides:view', context_system::instance());
So only users with that capability will have access to the page.
EDIT:
You can add a link via settings.php using something like
defined('MOODLE_INTERNAL') || die;
if ($hassiteconfig) {
$page = new admin_externalpage(
'local_webguides',
get_string('pluginname', 'local_webguides'),
new moodle_url('/local/webguides/index.php'),
'local/webguides:view'
);
$ADMIN->add('localplugins', $page);
}
Then in your index page ad this
require_once($CFG->libdir.'/adminlib.php');
and remove require_login() and require_capability() and replace with
admin_externalpage_setup('local_webguides');

local wordpress using remote database, ubuntu, apache2

im trying to connect my local wordpress install to a db on a domain i own using the code provided here:
https://coderwall.com/p/ck8v4a/remote-database-with-local-wordpress-instance
define('WP_CACHE', true); $currenthost = $_SERVER['HTTP_HOST'];
$mypos = strpos($currenthost, 'localhost/wpdir');
if ($mypos === false) {
define('WP_HOME','http://example.org/wpDir/');
define('WP_SITEURL','http://example.org/wpDir/');
} else {
define('WP_HOME','http://localhost');
define('WP_SITEURL','http://localhost/wpDir/');
}
with the above config i can open the site locally, it loads the navigation and recent post names correctly, so the db-connection seems to work.
i cannot use links and pages are not found though.
if i add my /wpDir/ to the define('WP_HOME'... and/or strpos($currenthost,... it gets redirected to http://localhost/wpDir/home/ (as it should) but i get an URL not found error.
my localhost dir is standard /var/www/html this is where my local wordpress installations are.
any ideas how to fix this?
update: i am back to working on this and it would really help a lot if i could manage to use the remote database for my local testing
i think it may be some kind of url rewriting problem but all my efforts to find a solution did not work...
not sure why it was so hard to find but i managed to do it (another way) basically by following the wp-codex here:
https://codex.wordpress.org/Running_a_Development_Copy_of_WordPress
using the drop-in method:
the code in my db.php file looks like this:
<?php
// paste this in a (new) file, wp-content/db.php
add_filter ( 'pre_option_home', 'test_localhosts' );
add_filter ( 'pre_option_siteurl', 'test_localhosts' );
function test_localhosts( ) {
if (strcasecmp($_SERVER['REQUEST_URI'], '/localCopyOfSite') == 0
|| strcasecmp(substr($_SERVER['REQUEST_URI'], 0, 17), '/localCopyOfSite/') == 0) {
return "http://localhost/localCopyOfSite/";
}
else return false; // act as normal; will pull main site info from db
}

Restrict content elements with IP in TYPO3

My requirement is to restrict a content element with IP of a specific country (Eg: Austria). That means people visiting the website from Austrian IPs should be visible the content element and for all other users, it should be hidden. I am using geoip solution to check the country. I wrote a user function to implement this feature. I wrote a small extension and set hidden flag 1 and 0 based on countries. But due to TYPO3 caching, I want to clear the cache everytime to reflect the changes in frontend. I included the extension as USER_INT, and extension is non-cachable. But unfortunately not working. Functionality working, but due to caching changes not reflect in realtime.
$uid = 175; // uid of the content element needs to be hidden
$geoplugin = new \geoPlugin();
$geoplugin->locate();
$countryCode = $geoplugin->countryCode;
if( $countryCode == 'AT' ){
$GLOBALS['TYPO3_DB']->exec_UPDATEquery('tt_content', 'uid IN ('.$uid.')', array('hidden' => 0));
}else{
$GLOBALS['TYPO3_DB']->exec_UPDATEquery('tt_content', 'uid IN ('.$uid.')', array('hidden' => 1));
}
Is there any method available in TYPO3 to restrict content element for specific IP / Countries? or can you guys suggest a solution to fix this please?
The solution of Jost is much less dirty than hiding the element in the database depending on the visitors country. By your way the database probably changed on every user visit.
Just create a micro extension.

how to make queries to db in custom script using zen cart?

hi i have a custom script that I call with ajax to retrieve some db info but for some reason it will not allow me to make the calls from this file. yet when i put the code in a page in the templates diretory lets say tpl_products_all_default.php they run fine. what do i need to do to be able to run queries from a custom script?
$sql = "select products_model from products where products_model = :productMdel:";
$sql = $db->bindVars($sql, ':productMdel:', 'C021', 'string');
$result = $db->Execute($sql);
if ($result->RecordCount() > 0) {
echo 'Model number = ' . $result->fields['products_model'];
} else {
echo 'Sorry, no record found for product number ' . $theProductId;
}
I may have an answer for you, though I'll admit it's not an optimal setup as it requires your custom file to be placed into the root directory.
If your custom file is being placed in the root (/your_custom_file.php) you can do the following to get access to the $db with the following require statement:
require('includes/application_top.php');
This will initialize all of the globals, and also call the includes/initsystem.php, which will spin through the values in the autoloader and include each script. The auto_loader can be viewed at includes/auto_loaders/config.core.php. In v1.5, you can see it finally includes the init_database.php script on lines 81-82. The init_database.php file finally initializes $db.
I initially ran into the same issue you had, and almost missed this setup because I had originally added my custom files to a custom directory like /my_module_extensions/my_file.php which failed. It seems the application_top.php loads everything with relative paths, so when executing under a directory other than the root, it would fail.
I hope this helps!
EDIT: Originally thought you were talking about an admin customization. I reworded this to relate to the public side. This also works from the admin side, if you need to extend the admin console.
Not true you can use an include from anywhere.
such as:
require_once('inc/php/application_top.php');
or
require_once('http://example.com/inc/php/application_top.php');

Wordpress Future Page Issue (Not Showing)

I've been asked to fix a problem on a wordpress site. The problem is caused by the fact posts should of been used instead of pages. However the site is up and running and quickest option would be to fix the problem using a hack of sorts.
The site has an events section, each event is a page (this is the issue, should really be a post). In order to have upcoming and past events the post date is used, therefore upcoming events has a post status of 'future'.
There's are list page of events which shows them fine by using the correct query_post(). Although the problem arises when you click through to actual event (which is a future page). If your logged in as an Admin then the page shows but if your not logged in you get a 404 page.
Now if they where posts then the "The Future is Now!" plugin would solve this problem. I have a feeling the only way round the problem is to rewrite part of core Wordpress files.
Any advice would be great, I have a lot of experience with Wordpress so even if you can point me in the right direction.
Cheers,
Jason
[Update]
Thanks maiorano84 for your detailed response. In the long run I intend to move them to posts but in the meantime they've requested we fix it asap without changing any urls (today they sent out a mass email with a list of events without checking any links)
Your solution of including post_status future doesn't work in this case as wordpress doesn't get to the stage of loading the template. Something in wordpress core stops it from getting that far. Ideally if they was a hook I could use to override this behavior in the meantime that would be excellent but if it comes to it I will temporarily edit the core files.
[Update 2]
I now know the two functions that need editing or use a hook that relates to them.
First of we is_404() which needs changed to not add future pages as 404
Second we have is_page() need to return true if a page is future
[Update 3]
I've found how to do this in the core. If you go to wp-includes/query.php line 2682. Copy this in instead of the old function it works correct. If you have a better way please let me know. Thanks.
/** Future Pages **/
// Check post status to determine if post should be displayed.
if ( !empty($this->posts) && ($this->is_single || $this->is_page) ) {
$status = get_post_status($this->posts[0]);
$post_status_obj = get_post_status_object($status);;
if ( !$post_status_obj->public ) {
if ( $post_status_obj->protected ) {
$this->is_preview = true;
print_r($status);
if ( 'draft' != $status ) {
$this->posts[0]->post_date = current_time('mysql');
} else {
$this->posts = array();
}
} elseif ( $post_status_obj->private ) {
if ( ! current_user_can($read_cap, $this->posts[0]->ID) )
$this->posts = array();
} else {
$this->posts = array();
}
}
/** END **/
You can actually add a post_status parameter of 'future' to your page queries. You REALLY shouldn't be modifying your core files in order to do what you want. So on your page.php and archive.php templates (or other relevant templates that's controlling your Event display), you can do something like this:
<?php
$params = array('posts_per_page'=>-1, 'post_status'=>'publish,future');
$query = new WP_Query($params);
if($query->have_posts()) : while($query->have_posts()) : $query->the_post();
?>
<!-- YOUR PAGE HTML HERE -->
<?php
endwhile;endif;
wp_reset_postdata();
?>
This is an oversimplification, but using the correct queries in the corresponding files will allow you to display your pages however you like.
More information here: http://codex.wordpress.org/Class_Reference/WP_Query
Another thing worth considering, and I realize that this wasn't a part of your question, but may very well solve your issue:
Why not create a subdomain on your client's server where you can work on fixing everything without interrupting the user experience? You can easily either import the existing database into your development environment, and make all the changes you need without affecting the live version.
Food for thought. My advice would be to nip this in the bud, and convert the pages over to posts as soon as possible, otherwise the website will turn into a giant mess very quickly, but that's your call.
Hope this helps.
UPDATE:
I would still advise against altering the core files, but if it's a last resort until everything gets fixed, then have at it. Here's is a "WP Friendly" solution I think might help:
add_action('wp','future_redirect', 0);
function future_redirect($WP_Object)
{
$page = is_page() ? get_page(get_the_ID()) : false;
if($page && $page->post_status == 'future')
{
wp_redirect(/*YOUR REDIRECT URL*/);
exit();
}
return $WP_Object;
}

Categories