Skip to content

Commit

Permalink
Merge pull request #132 from dragomano/develop
Browse files Browse the repository at this point in the history
Update to 2.1.2
  • Loading branch information
dragomano authored Apr 10, 2023
2 parents b6488f6 + 527421b commit 0d1fac8
Show file tree
Hide file tree
Showing 135 changed files with 2,776 additions and 2,490 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
![PHP](https://img.shields.io/badge/PHP-^8.0-blue.svg?style=flat)
[![Crowdin](https://badges.crowdin.net/light-portal/localized.svg)](https://crowdin.com/project/light-portal)

* **Tested on:** PHP 8.2.0 / MariaDB 10.6.7, MySQL 8.0.32, PostgreSQL 15.2
* **Tested on:** PHP 8.2.4 / MariaDB 10.11.2, MySQL 8.0.32, PostgreSQL 15.2
* **Languages:** Russian, English, Polish, Spanish, French, Turkish, Ukrainian, German, Italian, Portuguese, Greek
* [![](https://img.shields.io/badge/Demo-Forum-brightgreen.svg)](https://demo.dragomano.ru) [![](https://img.shields.io/badge/Docs-Site-orange.svg)](https://dragomano.github.io/Light-Portal/)

Expand Down
11 changes: 6 additions & 5 deletions Sources/LightPortal/AbstractMain.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
namespace Bugo\LightPortal;

use Bugo\LightPortal\Entities\Block;
use Bugo\LightPortal\Lists\IconList;

if (! defined('SMF'))
die('No direct access...');
Expand Down Expand Up @@ -58,13 +57,15 @@ protected function defineVars(): void
$this->context['lp_frontpage_topics'] = empty($this->modSettings['lp_frontpage_topics']) ? [] : explode(',', $this->modSettings['lp_frontpage_topics']);

$this->context['lp_header_panel_width'] = empty($this->modSettings['lp_header_panel_width']) ? 12 : (int) $this->modSettings['lp_header_panel_width'];
$this->context['lp_left_panel_width'] = empty($this->modSettings['lp_left_panel_width']) ? ['md' => 3, 'lg' => 3, 'xl' => 2] : $this->jsonDecode($this->modSettings['lp_left_panel_width'], true, false);
$this->context['lp_right_panel_width'] = empty($this->modSettings['lp_right_panel_width']) ? ['md' => 3, 'lg' => 3, 'xl' => 2] : $this->jsonDecode($this->modSettings['lp_right_panel_width'], true, false);
$this->context['lp_left_panel_width'] = empty($this->modSettings['lp_left_panel_width']) ? ['lg' => 3, 'xl' => 2] : $this->jsonDecode($this->modSettings['lp_left_panel_width'], true, false);
$this->context['lp_right_panel_width'] = empty($this->modSettings['lp_right_panel_width']) ? ['lg' => 3, 'xl' => 2] : $this->jsonDecode($this->modSettings['lp_right_panel_width'], true, false);
$this->context['lp_footer_panel_width'] = empty($this->modSettings['lp_footer_panel_width']) ? 12 : (int) $this->modSettings['lp_footer_panel_width'];

$this->context['lp_panel_direction'] = $this->jsonDecode($this->modSettings['lp_panel_direction'] ?? '', true, false);
$this->context['lp_active_blocks'] = (new Block)->getActive();
$this->context['lp_icon_set'] = (new IconList)->getAll();

$this->context['lp_active_blocks'] = (new Block)->getActive();

$this->context['lp_icon_set'] = $this->getEntityList('icon');
}

protected function loadAssets(): void
Expand Down
179 changes: 108 additions & 71 deletions Sources/LightPortal/Addons/AdsBlock/AdsBlock.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,18 @@
* @license https://spdx.org/licenses/GPL-3.0-or-later.html GPL-3.0-or-later
*
* @category addon
* @version 04.04.23
* @version 09.04.23
*/

namespace Bugo\LightPortal\Addons\AdsBlock;

use Bugo\LightPortal\Addons\Plugin;
use Bugo\LightPortal\Addons\Block;
use Bugo\LightPortal\Partials\{BoardSelect, PageSelect, TopicSelect};

if (! defined('LP_NAME'))
die('No direct access...');

class AdsBlock extends Plugin
class AdsBlock extends Block
{
public string $icon = 'fas fa-ad';

Expand All @@ -34,11 +35,12 @@ public function blockOptions(array &$options)
$options['ads_block']['content'] = 'html';

$options['ads_block']['parameters'] = [
'loader_code' => '',
'ads_placement' => '',
'included_boards' => '',
'included_topics' => '',
'end_date' => '',
'loader_code' => '',
'ads_placement' => '',
'include_boards' => '',
'include_topics' => '',
'include_pages' => '',
'end_date' => '',
];
}

Expand All @@ -53,20 +55,19 @@ public function validateBlockData(array &$parameters, string $type)
if ($type !== 'ads_block')
return;

$parameters['loader_code'] = FILTER_UNSAFE_RAW;
$parameters['ads_placement'] = FILTER_DEFAULT;
$parameters['included_boards'] = FILTER_DEFAULT;
$parameters['included_topics'] = FILTER_DEFAULT;
$parameters['end_date'] = FILTER_DEFAULT;
$parameters['loader_code'] = FILTER_UNSAFE_RAW;
$parameters['ads_placement'] = FILTER_DEFAULT;
$parameters['include_boards'] = FILTER_DEFAULT;
$parameters['include_topics'] = FILTER_DEFAULT;
$parameters['include_pages'] = FILTER_DEFAULT;
$parameters['end_date'] = FILTER_DEFAULT;
}

public function prepareBlockFields()
{
if ($this->context['lp_block']['type'] !== 'ads_block')
return;

$this->prepareTopicList();

$this->context['posting_fields']['loader_code']['label']['text'] = $this->txt['lp_ads_block']['loader_code'];
$this->context['posting_fields']['loader_code']['input'] = [
'type' => 'textarea',
Expand Down Expand Up @@ -105,17 +106,29 @@ public function prepareBlockFields()
$this->context['posting_fields']['ads_placement']['input']['html'] = '<div id="ads_placement" name="ads_placement"></div>';
$this->context['posting_fields']['ads_placement']['input']['tab'] = 'access_placement';

$this->context['posting_fields']['included_boards']['label']['html'] = '<label for="included_boards">' . $this->txt['lp_ads_block']['included_boards'] . '</label>';
$this->context['posting_fields']['included_boards']['input']['html'] = '<div id="included_boards" name="included_boards"></div>';
$this->context['posting_fields']['included_boards']['input']['tab'] = 'access_placement';

$this->context['lp_selected_boards'] = $this->getBoardList();

$this->context['posting_fields']['included_topics']['label']['html'] = '<label for="included_topics">' . $this->txt['lp_ads_block']['included_topics'] . '</label>';
$this->context['posting_fields']['included_topics']['input']['html'] = '<div id="included_topics" name="included_topics"></div>';
$this->context['posting_fields']['included_topics']['input']['tab'] = 'access_placement';

$this->context['lp_selected_topics'] = $this->getSelectedTopics();
$this->context['posting_fields']['include_boards']['label']['html'] = '<label for="include_boards">' . $this->txt['lp_ads_block']['include_boards'] . '</label>';
$this->context['posting_fields']['include_boards']['input']['tab'] = 'access_placement';
$this->context['posting_fields']['include_boards']['input']['html'] = (new BoardSelect)([
'id' => 'include_boards',
'hint' => $this->txt['lp_ads_block']['include_boards_select'],
'value' => $this->context['lp_block']['options']['parameters']['include_boards'] ?? '',
]);

$this->context['posting_fields']['include_topics']['label']['html'] = '<label for="include_topics">' . $this->txt['lp_ads_block']['include_topics'] . '</label>';
$this->context['posting_fields']['include_topics']['input']['tab'] = 'access_placement';
$this->context['posting_fields']['include_topics']['input']['html'] = (new TopicSelect)([
'id' => 'include_topics',
'hint' => $this->txt['lp_ads_block']['include_topics_select'],
'value' => $this->context['lp_block']['options']['parameters']['include_topics'] ?? '',
]);

$this->context['posting_fields']['include_pages']['label']['html'] = '<label for="include_pages">' . $this->txt['lp_ads_block']['include_pages'] . '</label>';
$this->context['posting_fields']['include_pages']['input']['tab'] = 'access_placement';
$this->context['posting_fields']['include_pages']['input']['html'] = (new PageSelect)([
'id' => 'include_pages',
'hint' => $this->txt['lp_ads_block']['include_pages_select'],
'value' => $this->context['lp_block']['options']['parameters']['include_pages'] ?? '',
]);

$this->context['posting_fields']['end_date']['label']['html'] = '<label for="end_date">' . $this->txt['lp_ads_block']['end_date'] . '</label>';
$this->context['posting_fields']['end_date']['input']['html'] = '
Expand Down Expand Up @@ -160,7 +173,7 @@ public function menuButtons()
{
$this->context['lp_block_placements']['ads'] = $this->txt['lp_ads_block']['ads_type'];

if (empty($this->context['current_board']) || $this->request()->is('xml'))
if ((empty($this->context['current_board']) && empty($this->context['lp_page'])) || $this->request()->is('xml'))
return;

$this->context['lp_ads_blocks'] = $this->getData();
Expand Down Expand Up @@ -188,7 +201,7 @@ public function adminAreas()
if ($this->request()->has('area') && $this->request('area') === 'lp_blocks')
$this->setTemplate()->withLayer('ads_block_form');

if ($this->request()->has('area') && $this->request('area') === 'lp_settings' && $this->request()->has('sa') && $this->request('sa') === 'panels') {
if ($this->request()->has('area') && $this->request('area') === 'lp_settings' && $this->context['current_subaction'] === 'panels') {
unset($this->context['lp_block_placements']['ads']);

$this->context['lp_block_placements'] = array_merge($this->context['lp_block_placements'], $this->getPlacements());
Expand Down Expand Up @@ -222,6 +235,18 @@ public function displayButtons()
$this->setTemplate()->withLayer('ads_placement_topic');
}

/**
* Display ads within portal pages
*
* Отображаем рекламу на страницах портала
*
* @hook preparePageData (portal)
*/
public function preparePageData()
{
$this->setTemplate()->withLayer('ads_placement_page');
}

/**
* Display ads within posts
*
Expand Down Expand Up @@ -351,29 +376,67 @@ private function getByPosition(string $position): array
if (empty($position))
return [];

return array_filter($this->context['lp_blocks']['ads'], function ($block) use ($position) {
if (! empty($block['parameters']['included_boards'])) {
$boards = array_flip(explode(',', $block['parameters']['included_boards']));
return array_filter(
$this->context['lp_blocks']['ads'],
fn($block) => (
$this->filterByIncludedTopics($block) &&
$this->filterByIncludedBoards($block) &&
$this->filterByIncludedPages($block) &&
$this->filterByAdsPlacement($position, $block)
)
);
}

private function filterByIncludedTopics(array $block): bool
{
if (! empty($block['parameters']['include_topics']) && ! empty($this->context['current_topic'])) {
$topics = array_flip(explode(',', $block['parameters']['include_topics']));

if (! array_key_exists($this->context['current_board'], $boards))
return false;
if (! array_key_exists($this->context['current_topic'], $topics)) {
return false;
}
}

if (! empty($block['parameters']['included_topics']) && ! empty($this->context['current_topic'])) {
$topics = array_flip(explode(',', $block['parameters']['included_topics']));
return true;
}

if (! array_key_exists($this->context['current_topic'], $topics))
return false;
private function filterByIncludedBoards(array $block): bool
{
if (! empty($block['parameters']['include_boards']) && ! empty($this->context['current_board']) && empty($this->context['current_topic'])) {
$boards = array_flip(explode(',', $block['parameters']['include_boards']));

if (! array_key_exists($this->context['current_board'], $boards)) {
return false;
}
}

if ($block['parameters']['ads_placement']) {
$placements = array_flip(explode(',', $block['parameters']['ads_placement']));
return true;
}

private function filterByIncludedPages(array $block): bool
{
if (! empty($block['parameters']['include_pages']) && ! empty($this->context['lp_page'])) {
$pages = array_flip(explode(',', $block['parameters']['include_pages']));

return array_key_exists($position, $placements);
if (! array_key_exists($this->context['lp_page']['id'], $pages)) {
return false;
}
}

return false;
});
return true;
}

private function filterByAdsPlacement(string $position, array $block): bool
{
if ($block['parameters']['ads_placement']) {
$placements = array_flip(explode(',', $block['parameters']['ads_placement']));

if (! array_key_exists($position, $placements)) {
return false;
}
}

return true;
}

private function getPlacements(): array
Expand All @@ -391,7 +454,9 @@ private function getPlacements(): array
'after_first_post',
'after_every_first_post',
'after_every_last_post',
'after_last_post'
'after_last_post',
'page_top',
'page_bottom',
],
$this->txt['lp_ads_block']['placement_set']
);
Expand Down Expand Up @@ -427,32 +492,4 @@ private function isTopicNumRepliesLesserThanMinReplies(): bool
return isset($this->context['lp_ads_block_plugin']['min_replies'])
&& $this->context['topicinfo']['num_replies'] < (int) $this->context['lp_ads_block_plugin']['min_replies'];
}

private function getSelectedTopics(): array
{
if (empty($this->context['lp_block']['options']['parameters']['included_topics']))
return [];

$request = $this->smcFunc['db_query']('', '
SELECT t.id_topic, m.subject
FROM {db_prefix}topics AS t
INNER JOIN {db_prefix}messages AS m ON (m.id_msg = t.id_first_msg)
WHERE t.id_topic IN ({array_int:topics})',
[
'topics' => explode(',', $this->context['lp_block']['options']['parameters']['included_topics']),
]
);

$topics = [];
while ($row = $this->smcFunc['db_fetch_assoc']($request)) {
$this->censorText($row['subject']);

$topics[$row['id_topic']] = str_replace(array("'", "\""), "", $row['subject']);
}

$this->smcFunc['db_free_result']($request);
$this->context['lp_num_queries']++;

return $topics;
}
}
22 changes: 13 additions & 9 deletions Sources/LightPortal/Addons/AdsBlock/langs/english.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,19 @@

return [
'title' => 'Ads block',
'description' => 'Displays custom HTML code in boards and topics.',
'description' => 'Displays custom HTML code in boards, topics, and on portal pages.',
'min_replies' => 'Do not display ads in topics that have less than the specified number of replies',
'loader_code' => 'Loader code (between &lt;head&gt; and &lt;/head&gt; tags)',
'end_date' => 'End date (the block will turn off automatically)',
'ads_type' => 'Boards and topics',
'included_boards' => 'Boards',
'included_boards_select' => 'Select boards to display ads only there. Otherwise, the block will be displayed in all boards.',
'included_topics' => 'Topics',
'included_topics_select' => 'Select topics to display ads only there. Otherwise, the block will be displayed in all topics.',
'ads_type' => 'Advertising in boards, topics and portal pages',
'include_boards' => 'Boards',
'include_boards_select' => 'Select boards to display ads only there. Otherwise, the block will be displayed in all boards.',
'include_topics' => 'Topics',
'include_topics_select' => 'Select topics to display ads only there. Otherwise, the block will be displayed in all topics.',
'include_pages' => 'Pages',
'include_pages_select' => 'Select portal pages to display ads only there. Otherwise, the block will be displayed in all pages.',
'select_placement' => 'Where should the block be displayed?',
'placement_set' => array(
'placement_set' => [
'At the top of boards',
'At the bottom of boards',
'At the top of topics',
Expand All @@ -24,7 +26,9 @@
'After the first message',
'After each first message on the page',
'After each last message on the page',
'After the last message'
),
'After the last message',
'At the top of portal pages',
'At the bottom of portal pages',
],
'no_ads_placement' => 'The <strong>placement</strong> field was not filled out. It is required.',
];
Loading

0 comments on commit 0d1fac8

Please sign in to comment.