API for Support by other plugins (Lightbox with Photoswipe)

[ad_1]

I came up with another solution. Just let me know, if this is OK for you or if you plan to change the options structure at any time.

The whole procedure can be disabled by an option in the backend settings, just in case this won’t work any longer with future updates of your plugin.

First during initialization I will check if your plugin is active and if this is the case, I will use the mappings option, so I know if there any domains mappings defined:

class LightboxPhotoSwipe
{
    // ...
    private $domainMappings;
    // ...

    public function __construct($pluginFile)
    {
        // ...
        $this->domainMappings = false;
        if (is_plugin_active('multiple-domain-mapping-on-single-site/multidomainmapping.php')) {
            $this->domainMappings = get_option('falke_mdm_mappings');
        }
        // ...
    }

The structure I identified for the settings is, that there is an array mappings which contains a set of three values for each domain. Therefore to check wether an image link may need to get fixed internally before working with it for getting image meta data etc., I will do the following:

// $baseurlHttp contains the base URL of the
// website - e.g. "https://my-site.example"

if ($this->domainMappings !== false && is_array($this->domainMappings['mappings'])) {
    if (substr($file, 0, 7) === 'http://') {
         $fileSchemaPrefix = 'http://';
    } else {
         $fileSchemaPrefix = 'https://';
    }
    foreach ($this->domainMappings['mappings'] as $mapping) {
        if (isset($mapping['domain'])) {
            $mappingBaseUrl = $fileSchemaPrefix.$mapping['domain'];
            $length = strlen($mappingBaseUrl);
            if ($length>0 && substr($file, 0, $length) === $mappingBaseUrl) {
                $file = $baseurlHttp.'/'.ltrim(substr($file, $length),'/');
            }
        }
    }
}

hy arno,
thanks for getting in touch 🙂

it’s absolutely fine to use the mappings-option for that purpose. by now we do not have any plans for refacturing our code or options.

best,
matt

 

This site will teach you how to build a WordPress website for beginners. We will cover everything from installing WordPress to adding pages, posts, and images to your site. You will learn how to customize your site with themes and plugins, as well as how to market your site online.

Buy WordPress Transfer