Plugin Directory

Changeset 2381426


Ignore:
Timestamp:
09/14/2020 09:09:27 PM (5 years ago)
Author:
dottech
Message:

Version 1.0.3

Location:
dot-monetize-polls-quizzes/trunk
Files:
5 edited

Legend:

Unmodified
Added
Removed
  • dot-monetize-polls-quizzes/trunk/dot-press.php

    r2380199 r2381426  
    33 * Plugin Name:       DOT | Monetize Polls & Quizzes
    44 * Description:      The easiest way to create and publish interactive polls and quizzes. Fully integrated monetization and analytics.
    5  * Version:           1.0.2
     5 * Version:           1.0.3
    66 * Requires at least: 5.2
    77 * Requires PHP:      7.2
     
    1717}
    1818
    19 define( 'DOT_PRESS_VERSION', '1.0.2' );
     19define( 'DOT_PRESS_VERSION', '1.0.3' );
    2020require_once plugin_dir_path(__FILE__) . 'constants.php';
    2121
  • dot-monetize-polls-quizzes/trunk/includes/class-dot-press-array-utils.php

    r2380191 r2381426  
    2323        }, $arr);
    2424    }
     25
     26    public static function findIndex($callable, $arr) {
     27        $index = -1;
     28
     29        foreach (self::ensureArray($arr) as $item) {
     30            $index++;
     31            $found = call_user_func($callable, $item);
     32
     33            if ($found) {
     34                return $index;
     35            }
     36        }
     37
     38        return -1;
     39    }
    2540}
  • dot-monetize-polls-quizzes/trunk/includes/class-dot-press-block-renderer.php

    r2380199 r2381426  
    44{
    55    private $post;
    6     /**
    7      * @var DotPressBlockCounter
    8      */
    9     private $counter;
    10 
    116    private $categories;
    127    private $tags;
     
    149    private $posts;
    1510
    16     public function __construct($post, $counter)
     11    public function __construct($post)
    1712    {
    1813        $this->post = $post;
    19         $this->counter = $counter;
    2014        $this->categories = DotPressArrayUtils::ensureArray(wp_get_post_categories($this->post->ID, ['fields' => 'ids']));
    2115        $this->tags = DotPressArrayUtils::ensureArray(wp_get_post_tags($this->post->ID, ['fields' => 'ids']));
     
    7064    }
    7165
    72     private function getType($placement) {
     66    private function getType($placement)
     67    {
    7368        $insertion = $placement->meta['insertion_point'][0];
    7469
     
    8479    }
    8580
    86     private function renderAfterLast($block_type, $placement, $carry) {
    87         global $post;
     81    private function renderAfterLast($block_type, $placement, $blocks, $currentIndex, $carry)
     82    {
    8883        $type = $this->getType($placement);
    8984
     
    9287        }
    9388
    94         $blocks = parse_blocks($post->post_content);
    9589        $lastIndexOf = 0;
    9690
     
    107101
    108102
    109 
    110         if ($this->counter->get_counter($block_type) === $lastIndexOf) {
     103        if ($currentIndex === $lastIndexOf) {
    111104            return $carry . $this->renderWrapper($placement, $this->renderFormats($placement));
    112105        }
     
    115108    }
    116109
     110    private function getBlockIndex($block, $blocks)
     111    {
     112        $blocksOfType = array_filter($blocks, function ($b) use ($block) {
     113            return $b['blockName'] === $block['blockName'];
     114        });
     115       
     116        return DotPressArrayUtils::findIndex(function ($b) use ($block) {
     117            return $b['innerHTML'] === $block['innerHTML'];
     118        }, $blocksOfType);
     119    }
     120
    117121    public function render($block_type, $block_content, $block)
    118122    {
    119 
    120123        if ($block_type) {
    121             // todo: feels like this shouldn't be here, but not sure how else might be better? Need to think about it
    122             $this->counter->count($block_type);
    123124            if (empty($this->posts)) {
    124125                return $block_content;
    125126            }
    126127
    127             $dotCounter = $this->counter;
    128             return array_reduce($this->posts, function ($carry, $placement) use ($dotCounter, $block_type) {
     128            global $post;
     129            $blocks = parse_blocks($post->post_content);
     130            // * +1 as the placements positions start at 1
     131            $index = $this->getBlockIndex($block, $blocks) + 1;
     132            return array_reduce($this->posts, function ($carry, $placement) use ($block_type, $blocks, $index) {
    129133                $possibleInsertionPoints = ['before-paragraph', 'after-paragraph', 'before-image', 'after-image'];
    130134                $insertionPointBlockTypes = [
     
    139143
    140144                if (preg_match('/after-last/', $insertionPoint)) {
    141                     return $this->renderAfterLast($block_type, $placement, $carry);
     145                    return $this->renderAfterLast($block_type, $placement, $blocks, $index, $carry);
    142146                }
    143147
     
    148152                }
    149153
    150                 $counter = $dotCounter->get_counter($insertionPointBlockType);
    151 
    152154                if (in_array($insertionPoint, $possibleInsertionPoints)) {
    153155                    $insertionNumber = intval($placement->meta['insertion_number'][0]);
    154156
    155                     if ($insertionNumber === $counter) {
     157                    if ($insertionNumber === $index) {
    156158                        $widgetTrigger = $this->renderWrapper($placement, $this->renderFormats($placement));
    157159
     
    173175    }
    174176
    175     private function renderWrapper($placement, $carry) {
     177    private function renderWrapper($placement, $carry)
     178    {
    176179        $marginOptions = [
    177180            'left' => '0 auto 0 0',
     
    183186    }
    184187
    185     private function renderFormats($placement) {
     188    private function renderFormats($placement)
     189    {
    186190        $widget_id = $placement->meta['widget_id'][0];
    187191        $format = explode(',', $placement->meta['format'][0]);
    188         return array_reduce($format, function($acc, $type) use ($widget_id) {
     192        return array_reduce($format, function ($acc, $type) use ($widget_id) {
    189193            return $acc . $this->renderScript($type, $widget_id);
    190194        }, '');
  • dot-monetize-polls-quizzes/trunk/includes/class-dot-press-public.php

    r2380199 r2381426  
    4444
    4545    /**
    46      * @var DotPressBlockCounter
    47      */
    48     private $counter;
    49 
    50     /**
    5146     * @var DotPressBlockRenderer
    5247     */
     
    6661        $this->version = $version;
    6762        $this->loader = $loader;
    68         $this->counter = new DotPressBlockCounter();
    6963
    7064        $this->loader->add_action( 'wp_enqueue_scripts', $this, 'enqueue_scripts' );
     
    9993        if (DotPressWpUtils::is_front_end()) {
    10094            global $post;
    101             $this->renderer = new DotPressBlockRenderer($post, $this->counter);
     95            $this->renderer = new DotPressBlockRenderer($post);
    10296        }
    10397    }
  • dot-monetize-polls-quizzes/trunk/readme.txt

    r2380199 r2381426  
    8585= 1.0.2 =
    8686* Stopped placement renderer from triggering when block rendering outside wp loop
     87
     88= 1.0.3 =
     89* Tweaked placement render logic to be less prone to external plugin block usage
Note: See TracChangeset for help on using the changeset viewer.