- Reaction score
- 48
- Points
- 55
Easy way to merge your products' copyright footers in a single line
This is a quick and dirty tutorial for how to automatically merge all copyright footers from your products into a single line, avoiding a situation where you have 5 mods released with 5 separate linesTemplate Modifications
Template: PAGE_CONTAINER
Modification Key: your_namespace_copyright_footer_display
Description: Copyright Footer
Search type: PHP Callback
Find: #({{ phrase\('extra_copyright'\) }})#si
Replace: Your\Namespace\Template\Callback\Copyright::insert
Execution order: 10
Code Event Listeners
Listen to event: app_setup
Execute callback: Your\Namespace\Listener :: appSetup
Execution order: 10
Description: App setup
PHP
/src/Your/Namespace/Listener.php
PHP:
<?php
namespace Your\Namespace;
class Listener
{
/**
* The product ID (in the store)
* @var integer
*/
protected static $_productId = 0;
/**
* @param \XF\App $app
*/
public static function appSetup(
\XF\App $app
)
{
/*PREFIX_BRANDING_START*/
if (!$app->offsetExists('prefix_branding_free'))
{
// Make sure we fetch the branding array from the application
$branding = $app->offsetExists('prefix_branding') ? $app->prefix_branding : [];
// Add productid to the array
$branding[] = self::$_productId;
// Store the branding
$app->prefix_branding = $branding;
}
/*PREFIX_BRANDING_END*/
}
}
(Replace prefix with the vendor prefix (e.g. dbtech is ours, normally it's the "your" part of the "Your\Namespace" replacement.)
/src/Your/Namespace/Template/Callback/Copyright.php
PHP:
<?php
namespace Your\Namespace\Template\Callback;
class Copyright
{
/**
* @param $matches
*
* @return string
*/
public static function insert($matches)
{
if (!isset($matches[1]))
{
// Clean error prevention
return '';
}
/** @var \XF\App $app */
$app = \XF::app();
$branding = $app->offsetExists('prefix_branding') ? $app->prefix_branding : [];
if (!count($branding) OR !is_array($branding))
{
// We had nothing left, another mod would have inserted the copyright footer
return $matches[1];
}
// Create this long string
$html = '<div>
Parts of this site powered by XenForo add-ons from YourCompany™
©2011-' . date('Y') . ' <a class="u-concealed" rel="nofollow" href="https://www.yoursite.com" target="_blank">YourCompany</a>
(<a class="u-concealed" rel="nofollow" href="https://www.yoursite.com/details.php?products=' . implode(',', $branding) . '" target="_blank">Details</a>)
</div>';
// Make sure we null this out
$app->prefix_branding = [];
return $matches[1] . $html;
}
}
(Replace prefix with the vendor prefix (e.g. dbtech is ours, normally it's the "your" part of the "Your\Namespace" replacement. Also, replace the www.yoursite.com links with the links to your actual website. Also, replace "YourCompany" with the name of your company. Lastly, either create the details.php page that accepts product IDs as parameters to display a list of the products that site is running, or remove that entire parenthesis section altogether.)
(Optional) DragonByte eCommerce Integration
If you are using DragonByte eCommerce and you use its "Generated" download handler, you can extend eCommerce to automatically strip branding.
This is intended to be a separate add-on, to run on your site where you have eCommerce installed!
This part of the guide assumes
a) You are using DragonByte eCommerce (duh ?)
b) You are familiar with how the "Generated" download handler works
d) You have configured your "parent product" like this:
Class Extension
Base class name: DBTech\eCommerce\Service\DownloadVersion\Generator
Extension class name: Your\Namespace\DBTech\eCommerce\Service\DownloadVersion\Generator
PHP
Your/Namespace/DBTech/eCommerce/Service/DownloadVersion/Generator.php
PHP:
<?php
namespace Your\Namespace\DBTech\eCommerce\Service\DownloadVersion;
use XF\Util\File;
/**
* Class Generator
*
* @package Your\Namespace\DBTech\eCommerce\Service\DownloadVersion
*/
class Generator extends XFCP_Generator
{
/**
* @param $replace
*/
protected function replaceTokens($replace)
{
parent::replaceTokens($replace);
$visitor = \XF::visitor();
$showBranding = true;
if ($this->license && ($this->version->Download->Product->branding_free || $this->version->Download->Product->global_branding_free))
{
/** @var \DBTech\eCommerce\Entity\License $childLicense */
foreach ($this->license->Children as $childLicense)
{
if ($childLicense->product_id == $this->version->Download->Product->branding_free)
{
$showBranding = false;
break;
}
}
if ($showBranding && $this->version->Download->Product->global_branding_free)
{
$hasGlobalBrandingFree = $this->finder('DBTech\eCommerce:License')
->where('product_id', $this->version->Download->Product->global_branding_free)
->fetchOne();
$showBranding = !$hasGlobalBrandingFree;
}
}
$filesIterator = new \RecursiveIteratorIterator(
new \RecursiveDirectoryIterator($this->uploadRoot, \RecursiveDirectoryIterator::SKIP_DOTS),
\RecursiveIteratorIterator::CHILD_FIRST
);
foreach ($filesIterator AS $file)
{
if ($file->isDir())
{
continue;
}
// skip hidden dot files, e.g. .DS_Store, .gitignore etc.
if (strpos($file->getFilename(), '.') === 0)
{
continue;
}
// Skip .lock files
if (!in_array($file->getExtension(), [
'php',
'js',
'xml'
]))
{
continue;
}
$output = trim(file_get_contents($file->getPathname()));
if (!$showBranding)
{
$output = preg_replace("#(/\*PREFIX_BRANDING_START\*/)(.*)(/\*PREFIX_BRANDING_END\*/)#sU", '', $output);
}
File::writeFile($file->getPathname(), $output, false);
}
}
}
(As above, replace PREFIX with your vendor prefix and Your\Namespace with whatever your namespace is for this custom add-on.)
PHP (Hypertext Preprocessor) is a popular server-side scripting language designed for web development but also used as a general-purpose programming language. It is especially suited for creating dynamic web pages and can be embedded into HTML.