Changeset 2381426
- Timestamp:
- 09/14/2020 09:09:27 PM (5 years ago)
- Location:
- dot-monetize-polls-quizzes/trunk
- Files:
-
- 5 edited
-
dot-press.php (modified) (2 diffs)
-
includes/class-dot-press-array-utils.php (modified) (1 diff)
-
includes/class-dot-press-block-renderer.php (modified) (11 diffs)
-
includes/class-dot-press-public.php (modified) (3 diffs)
-
readme.txt (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
-
dot-monetize-polls-quizzes/trunk/dot-press.php
r2380199 r2381426 3 3 * Plugin Name: DOT | Monetize Polls & Quizzes 4 4 * Description: The easiest way to create and publish interactive polls and quizzes. Fully integrated monetization and analytics. 5 * Version: 1.0. 25 * Version: 1.0.3 6 6 * Requires at least: 5.2 7 7 * Requires PHP: 7.2 … … 17 17 } 18 18 19 define( 'DOT_PRESS_VERSION', '1.0. 2' );19 define( 'DOT_PRESS_VERSION', '1.0.3' ); 20 20 require_once plugin_dir_path(__FILE__) . 'constants.php'; 21 21 -
dot-monetize-polls-quizzes/trunk/includes/class-dot-press-array-utils.php
r2380191 r2381426 23 23 }, $arr); 24 24 } 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 } 25 40 } -
dot-monetize-polls-quizzes/trunk/includes/class-dot-press-block-renderer.php
r2380199 r2381426 4 4 { 5 5 private $post; 6 /**7 * @var DotPressBlockCounter8 */9 private $counter;10 11 6 private $categories; 12 7 private $tags; … … 14 9 private $posts; 15 10 16 public function __construct($post , $counter)11 public function __construct($post) 17 12 { 18 13 $this->post = $post; 19 $this->counter = $counter;20 14 $this->categories = DotPressArrayUtils::ensureArray(wp_get_post_categories($this->post->ID, ['fields' => 'ids'])); 21 15 $this->tags = DotPressArrayUtils::ensureArray(wp_get_post_tags($this->post->ID, ['fields' => 'ids'])); … … 70 64 } 71 65 72 private function getType($placement) { 66 private function getType($placement) 67 { 73 68 $insertion = $placement->meta['insertion_point'][0]; 74 69 … … 84 79 } 85 80 86 private function renderAfterLast($block_type, $placement, $ carry) {87 global $post;81 private function renderAfterLast($block_type, $placement, $blocks, $currentIndex, $carry) 82 { 88 83 $type = $this->getType($placement); 89 84 … … 92 87 } 93 88 94 $blocks = parse_blocks($post->post_content);95 89 $lastIndexOf = 0; 96 90 … … 107 101 108 102 109 110 if ($this->counter->get_counter($block_type) === $lastIndexOf) { 103 if ($currentIndex === $lastIndexOf) { 111 104 return $carry . $this->renderWrapper($placement, $this->renderFormats($placement)); 112 105 } … … 115 108 } 116 109 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 117 121 public function render($block_type, $block_content, $block) 118 122 { 119 120 123 if ($block_type) { 121 // todo: feels like this shouldn't be here, but not sure how else might be better? Need to think about it122 $this->counter->count($block_type);123 124 if (empty($this->posts)) { 124 125 return $block_content; 125 126 } 126 127 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) { 129 133 $possibleInsertionPoints = ['before-paragraph', 'after-paragraph', 'before-image', 'after-image']; 130 134 $insertionPointBlockTypes = [ … … 139 143 140 144 if (preg_match('/after-last/', $insertionPoint)) { 141 return $this->renderAfterLast($block_type, $placement, $ carry);145 return $this->renderAfterLast($block_type, $placement, $blocks, $index, $carry); 142 146 } 143 147 … … 148 152 } 149 153 150 $counter = $dotCounter->get_counter($insertionPointBlockType);151 152 154 if (in_array($insertionPoint, $possibleInsertionPoints)) { 153 155 $insertionNumber = intval($placement->meta['insertion_number'][0]); 154 156 155 if ($insertionNumber === $ counter) {157 if ($insertionNumber === $index) { 156 158 $widgetTrigger = $this->renderWrapper($placement, $this->renderFormats($placement)); 157 159 … … 173 175 } 174 176 175 private function renderWrapper($placement, $carry) { 177 private function renderWrapper($placement, $carry) 178 { 176 179 $marginOptions = [ 177 180 'left' => '0 auto 0 0', … … 183 186 } 184 187 185 private function renderFormats($placement) { 188 private function renderFormats($placement) 189 { 186 190 $widget_id = $placement->meta['widget_id'][0]; 187 191 $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) { 189 193 return $acc . $this->renderScript($type, $widget_id); 190 194 }, ''); -
dot-monetize-polls-quizzes/trunk/includes/class-dot-press-public.php
r2380199 r2381426 44 44 45 45 /** 46 * @var DotPressBlockCounter47 */48 private $counter;49 50 /**51 46 * @var DotPressBlockRenderer 52 47 */ … … 66 61 $this->version = $version; 67 62 $this->loader = $loader; 68 $this->counter = new DotPressBlockCounter();69 63 70 64 $this->loader->add_action( 'wp_enqueue_scripts', $this, 'enqueue_scripts' ); … … 99 93 if (DotPressWpUtils::is_front_end()) { 100 94 global $post; 101 $this->renderer = new DotPressBlockRenderer($post , $this->counter);95 $this->renderer = new DotPressBlockRenderer($post); 102 96 } 103 97 } -
dot-monetize-polls-quizzes/trunk/readme.txt
r2380199 r2381426 85 85 = 1.0.2 = 86 86 * 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.