Newsletter - Version 6.5.6

Version Description

  • Improved post image extraction
Download this release

Release Info

Developer satollo
Plugin Icon 128x128 Newsletter
Version 6.5.6
Comparing to
See all releases

Code changes from version 6.5.5 to 6.5.6

Files changed (4) hide show
  1. includes/composer.php +190 -152
  2. includes/helper.php +33 -30
  3. plugin.php +2 -2
  4. readme.txt +3 -1
includes/composer.php CHANGED
@@ -264,6 +264,42 @@ class TNP_Composer {
264
  return $b;
265
  }
266
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
267
  }
268
 
269
  /**
@@ -271,65 +307,66 @@ class TNP_Composer {
271
  * Initialize class with max columns per row and start to add cells.
272
  */
273
  class TNP_Composer_Grid_System {
274
- /**
275
- * @var TNP_Composer_Grid_Row[]
276
- */
277
- private $rows;
278
-
279
- /**
280
- * @var int
281
- */
282
- private $cells_per_row;
283
-
284
- /**
285
- * @var int
286
- */
287
- private $cells_counter;
288
-
289
- /**
290
- * TNP_Composer_Grid_System constructor.
291
- *
292
- * @param int $columns_per_row Max columns per row
293
- */
294
- public function __construct( $columns_per_row ) {
295
- $this->cells_per_row = $columns_per_row;
296
- $this->cells_counter = 0;
297
- }
298
-
299
- public function __toString() {
300
- return $this->render();
301
- }
302
-
303
- /**
304
- * Add cell to grid
305
- *
306
- * @param TNP_Composer_Grid_Cell $cell
307
- */
308
- public function add_cell( $cell ) {
309
-
310
- if ( $this->cells_counter % $this->cells_per_row === 0 ) {
311
- $this->add_row( new TNP_Composer_Grid_Row() );
312
- }
313
-
314
- $row_idx = (int) floor( $this->cells_counter / $this->cells_per_row );
315
- $this->rows[ $row_idx ]->add_cell( $cell );
316
- $this->cells_counter ++;
317
-
318
- }
319
-
320
- private function add_row( $row ) {
321
- $this->rows[] = $row;
322
- }
323
-
324
- public function render() {
325
-
326
- $str = '';
327
- foreach ( $this->rows as $row ) {
328
- $str .= $row->render();
329
- }
330
-
331
- return $str;
332
- }
 
333
  }
334
 
335
  /**
@@ -337,44 +374,44 @@ class TNP_Composer_Grid_System {
337
  */
338
  class TNP_Composer_Grid_Row {
339
 
340
- /**
341
- * @var TNP_Composer_Grid_Cell[]
342
- */
343
- private $cells;
344
-
345
- public function __construct( ...$cells ) {
346
- if ( ! empty( $cells ) ) {
347
- foreach ( $cells as $cell ) {
348
- $this->add_cell( $cell );
349
- }
350
- }
351
- }
352
-
353
- /**
354
- * @param TNP_Composer_Grid_Cell $cell
355
- */
356
- public function add_cell( $cell ) {
357
- $this->cells[] = $cell;
358
- }
359
-
360
- public function render() {
361
- $rendered_cells = '';
362
- $column_percentage_width = round( 100 / $this->cells_count(), 0, PHP_ROUND_HALF_DOWN ) . '%';
363
- foreach ( $this->cells as $cell ) {
364
- $rendered_cells .= $cell->render( [ 'width' => $column_percentage_width ] );
365
- }
366
-
367
- $row_template = $this->get_template();
368
-
369
- return str_replace( 'TNP_ROW_CONTENT_PH', $rendered_cells, $row_template );
370
- }
371
-
372
- private function cells_count() {
373
- return count( $this->cells );
374
- }
375
-
376
- private function get_template() {
377
- return "<table border='0'
378
  cellpadding='0'
379
  cellspacing='0'
380
  width='100%'>
@@ -386,66 +423,66 @@ class TNP_Composer_Grid_Row {
386
  </tr>
387
  </tbody>
388
  </table>";
389
- }
 
390
  }
391
 
392
  /**
393
  * Class TNP_Composer_Grid_Cell
394
  */
395
  class TNP_Composer_Grid_Cell {
396
- /**
397
- * @var string
398
- */
399
- private $content;
400
-
401
- /**
402
- * @var array
403
- */
404
- public $args;
405
-
406
- public function __construct( $content = null, $args = [] ) {
407
- $default_args = [
408
- 'width' => '100%',
409
- 'class' => '',
410
- 'align' => 'left',
411
- 'valign' => 'top'
412
- ];
413
-
414
- $this->args = array_merge( $default_args, $args );
415
-
416
- $this->content = $content ? $content : '';
417
- }
418
-
419
- public function add_content( $content ) {
420
- $this->content .= $content;
421
- }
422
-
423
- public function render( $args ) {
424
- $this->args = array_merge( $this->args, $args );
425
-
426
- $column_template = $this->get_template();
427
- $column = str_replace(
428
- [
429
- 'TNP_ALIGN_PH',
430
- 'TNP_VALIGN_PH',
431
- 'TNP_WIDTH_PH',
432
- 'TNP_CLASS_PH',
433
- 'TNP_COLUMN_CONTENT_PH'
434
- ],
435
- [
436
- $this->args['align'],
437
- $this->args['valign'],
438
- $this->args['width'],
439
- $this->args['class'],
440
- $this->content
441
- ],
442
- $column_template );
443
-
444
- return $column;
445
- }
446
-
447
- private function get_template() {
448
- return "<table border='0'
449
  cellpadding='0'
450
  cellspacing='0'
451
  width='TNP_WIDTH_PH'
@@ -463,5 +500,6 @@ class TNP_Composer_Grid_Cell {
463
  </tr>
464
  </tbody>
465
  </table>";
466
- }
 
467
  }
264
  return $b;
265
  }
266
 
267
+ /**
268
+ * Returns a WP media ID for the specified post (or false if nothing can be found)
269
+ * looking for the featured image or, if missing, taking the first media in the gallery and
270
+ * if again missing, searching the first reference to a media in the post content.
271
+ *
272
+ * @param int $post_id
273
+ * @return int
274
+ */
275
+ static function get_post_thumbnail_id($post_id) {
276
+ if (is_object($post_id)) {
277
+ $post_id = $post_id->ID;
278
+ }
279
+
280
+ // Find a media id to be used as featured image
281
+ $media_id = get_post_thumbnail_id($post_id);
282
+ if (!empty($media_id)) {
283
+ return $media_id;
284
+ }
285
+
286
+ $attachments = get_children(array('numberpost' => 1, 'post_parent' => $post_id, 'post_status' => 'inherit', 'post_type' => 'attachment', 'post_mime_type' => 'image', 'order' => 'ASC', 'orderby' => 'menu_order'));
287
+ if (!empty($attachments)) {
288
+ foreach ($attachments as $id => &$attachment) {
289
+ return $id;
290
+ }
291
+ }
292
+
293
+ $post = get_post($post_id);
294
+
295
+ $r = preg_match('/wp-image-(\d+)/', $post->post_content, $matches);
296
+ if ($matches) {
297
+ return (int)$matches[1];
298
+ }
299
+
300
+ return false;
301
+ }
302
+
303
  }
304
 
305
  /**
307
  * Initialize class with max columns per row and start to add cells.
308
  */
309
  class TNP_Composer_Grid_System {
310
+
311
+ /**
312
+ * @var TNP_Composer_Grid_Row[]
313
+ */
314
+ private $rows;
315
+
316
+ /**
317
+ * @var int
318
+ */
319
+ private $cells_per_row;
320
+
321
+ /**
322
+ * @var int
323
+ */
324
+ private $cells_counter;
325
+
326
+ /**
327
+ * TNP_Composer_Grid_System constructor.
328
+ *
329
+ * @param int $columns_per_row Max columns per row
330
+ */
331
+ public function __construct($columns_per_row) {
332
+ $this->cells_per_row = $columns_per_row;
333
+ $this->cells_counter = 0;
334
+ }
335
+
336
+ public function __toString() {
337
+ return $this->render();
338
+ }
339
+
340
+ /**
341
+ * Add cell to grid
342
+ *
343
+ * @param TNP_Composer_Grid_Cell $cell
344
+ */
345
+ public function add_cell($cell) {
346
+
347
+ if ($this->cells_counter % $this->cells_per_row === 0) {
348
+ $this->add_row(new TNP_Composer_Grid_Row());
349
+ }
350
+
351
+ $row_idx = (int) floor($this->cells_counter / $this->cells_per_row);
352
+ $this->rows[$row_idx]->add_cell($cell);
353
+ $this->cells_counter ++;
354
+ }
355
+
356
+ private function add_row($row) {
357
+ $this->rows[] = $row;
358
+ }
359
+
360
+ public function render() {
361
+
362
+ $str = '';
363
+ foreach ($this->rows as $row) {
364
+ $str .= $row->render();
365
+ }
366
+
367
+ return $str;
368
+ }
369
+
370
  }
371
 
372
  /**
374
  */
375
  class TNP_Composer_Grid_Row {
376
 
377
+ /**
378
+ * @var TNP_Composer_Grid_Cell[]
379
+ */
380
+ private $cells;
381
+
382
+ public function __construct(...$cells) {
383
+ if (!empty($cells)) {
384
+ foreach ($cells as $cell) {
385
+ $this->add_cell($cell);
386
+ }
387
+ }
388
+ }
389
+
390
+ /**
391
+ * @param TNP_Composer_Grid_Cell $cell
392
+ */
393
+ public function add_cell($cell) {
394
+ $this->cells[] = $cell;
395
+ }
396
+
397
+ public function render() {
398
+ $rendered_cells = '';
399
+ $column_percentage_width = round(100 / $this->cells_count(), 0, PHP_ROUND_HALF_DOWN) . '%';
400
+ foreach ($this->cells as $cell) {
401
+ $rendered_cells .= $cell->render(['width' => $column_percentage_width]);
402
+ }
403
+
404
+ $row_template = $this->get_template();
405
+
406
+ return str_replace('TNP_ROW_CONTENT_PH', $rendered_cells, $row_template);
407
+ }
408
+
409
+ private function cells_count() {
410
+ return count($this->cells);
411
+ }
412
+
413
+ private function get_template() {
414
+ return "<table border='0'
415
  cellpadding='0'
416
  cellspacing='0'
417
  width='100%'>
423
  </tr>
424
  </tbody>
425
  </table>";
426
+ }
427
+
428
  }
429
 
430
  /**
431
  * Class TNP_Composer_Grid_Cell
432
  */
433
  class TNP_Composer_Grid_Cell {
434
+
435
+ /**
436
+ * @var string
437
+ */
438
+ private $content;
439
+
440
+ /**
441
+ * @var array
442
+ */
443
+ public $args;
444
+
445
+ public function __construct($content = null, $args = []) {
446
+ $default_args = [
447
+ 'width' => '100%',
448
+ 'class' => '',
449
+ 'align' => 'left',
450
+ 'valign' => 'top'
451
+ ];
452
+
453
+ $this->args = array_merge($default_args, $args);
454
+
455
+ $this->content = $content ? $content : '';
456
+ }
457
+
458
+ public function add_content($content) {
459
+ $this->content .= $content;
460
+ }
461
+
462
+ public function render($args) {
463
+ $this->args = array_merge($this->args, $args);
464
+
465
+ $column_template = $this->get_template();
466
+ $column = str_replace(
467
+ [
468
+ 'TNP_ALIGN_PH',
469
+ 'TNP_VALIGN_PH',
470
+ 'TNP_WIDTH_PH',
471
+ 'TNP_CLASS_PH',
472
+ 'TNP_COLUMN_CONTENT_PH'
473
+ ], [
474
+ $this->args['align'],
475
+ $this->args['valign'],
476
+ $this->args['width'],
477
+ $this->args['class'],
478
+ $this->content
479
+ ], $column_template);
480
+
481
+ return $column;
482
+ }
483
+
484
+ private function get_template() {
485
+ return "<table border='0'
 
486
  cellpadding='0'
487
  cellspacing='0'
488
  width='TNP_WIDTH_PH'
500
  </tr>
501
  </tbody>
502
  </table>";
503
+ }
504
+
505
  }
includes/helper.php CHANGED
@@ -42,24 +42,24 @@ function tnp_post_thumbnail_src($post, $size = 'thumbnail', $alternative = '') {
42
  return $media[0];
43
  }
44
 
45
- function tnp_post_excerpt( $post, $length = 30 ) {
46
- if ( empty( $post->post_excerpt ) ) {
47
- $excerpt = tnp_delete_all_shordcodes_tags( wp_strip_all_tags( $post->post_content ) );
48
- $excerpt = wp_trim_words( $excerpt, $length );
49
- } else {
50
- $excerpt = wp_trim_words( $post->post_excerpt, $length );
51
- }
52
-
53
- return $excerpt;
54
  }
55
 
56
- function tnp_delete_all_shordcodes_tags( $post_content = '' ) {
57
- //Delete open tags
58
- $post_content = preg_replace( "/\[[a-zA-Z0-9_-]*?(\s.*?)?\]/", '', $post_content );
59
- //Delete close tags
60
- $post_content = preg_replace( "/\[\/[a-zA-Z0-9_-]*?\]/", '', $post_content );
61
 
62
- return $post_content;
63
  }
64
 
65
  function tnp_post_permalink($post) {
@@ -156,7 +156,7 @@ function tnp_media_resize($media_id, $size) {
156
 
157
  return $uploads['baseurl'] . '/newsletter/thumbnails/' . $relative_thumb;
158
  }
159
-
160
  function _tnp_get_default_media($media_id, $size) {
161
 
162
  $src = wp_get_attachment_image_src($media_id, $size);
@@ -214,6 +214,7 @@ function tnp_resize($media_id, $size) {
214
  $editor = wp_get_image_editor($absolute_file);
215
  if (is_wp_error($editor)) {
216
  Newsletter::instance()->logger->error($editor);
 
217
  return _tnp_get_default_media($media_id, $size);
218
  }
219
 
@@ -228,6 +229,7 @@ function tnp_resize($media_id, $size) {
228
 
229
  if (is_wp_error($resized)) {
230
  Newsletter::instance()->logger->error($resized);
 
231
  return _tnp_get_default_media($media_id, $size);
232
  }
233
 
@@ -262,25 +264,26 @@ function tnp_resize($media_id, $size) {
262
  *
263
  * @return TNP_Media
264
  */
265
- function tnp_composer_block_posts_get_media( $post, $size, $default_image_url ) {
266
- $post_thumbnail_id = get_post_thumbnail_id( $post );
267
-
268
- if ( ! empty( $post_thumbnail_id ) ) {
269
- $media = tnp_resize( $post_thumbnail_id, array_values( $size ) );
270
- } else {
271
- $media = new TNP_Media();
272
- $media->url = $default_image_url;
273
- $media->width = $size['width'];
274
- $media->height = $size['height'];
275
- }
276
-
277
- return $media;
 
278
  }
279
 
280
  function tnp_outlook_wrapper_open($width = 600) {
281
  return NewsletterEmails::get_outlook_wrapper_open($width);
282
  }
283
-
284
  function tnp_outlook_wrapper_close() {
285
  return NewsletterEmails::get_outlook_wrapper_close();
286
  }
42
  return $media[0];
43
  }
44
 
45
+ function tnp_post_excerpt($post, $length = 30) {
46
+ if (empty($post->post_excerpt)) {
47
+ $excerpt = tnp_delete_all_shordcodes_tags(wp_strip_all_tags($post->post_content));
48
+ $excerpt = wp_trim_words($excerpt, $length);
49
+ } else {
50
+ $excerpt = wp_trim_words($post->post_excerpt, $length);
51
+ }
52
+
53
+ return $excerpt;
54
  }
55
 
56
+ function tnp_delete_all_shordcodes_tags($post_content = '') {
57
+ //Delete open tags
58
+ $post_content = preg_replace("/\[[a-zA-Z0-9_-]*?(\s.*?)?\]/", '', $post_content);
59
+ //Delete close tags
60
+ $post_content = preg_replace("/\[\/[a-zA-Z0-9_-]*?\]/", '', $post_content);
61
 
62
+ return $post_content;
63
  }
64
 
65
  function tnp_post_permalink($post) {
156
 
157
  return $uploads['baseurl'] . '/newsletter/thumbnails/' . $relative_thumb;
158
  }
159
+
160
  function _tnp_get_default_media($media_id, $size) {
161
 
162
  $src = wp_get_attachment_image_src($media_id, $size);
214
  $editor = wp_get_image_editor($absolute_file);
215
  if (is_wp_error($editor)) {
216
  Newsletter::instance()->logger->error($editor);
217
+ Newsletter::instance()->logger->error('File: ' . $absolute_file);
218
  return _tnp_get_default_media($media_id, $size);
219
  }
220
 
229
 
230
  if (is_wp_error($resized)) {
231
  Newsletter::instance()->logger->error($resized);
232
+ Newsletter::instance()->logger->error('File: ' . $absolute_file);
233
  return _tnp_get_default_media($media_id, $size);
234
  }
235
 
264
  *
265
  * @return TNP_Media
266
  */
267
+ function tnp_composer_block_posts_get_media($post, $size, $default_image_url) {
268
+ $post_thumbnail_id = TNP_Composer::get_post_thumbnail_id($post);
269
+
270
+ if (!empty($post_thumbnail_id)) {
271
+ $media = tnp_resize($post_thumbnail_id, array_values($size));
272
+ } else {
273
+ Newsletter::instance()->logger->error('Thumbnail id not found');
274
+ $media = new TNP_Media();
275
+ $media->url = $default_image_url;
276
+ $media->width = $size['width'];
277
+ $media->height = $size['height'];
278
+ }
279
+
280
+ return $media;
281
  }
282
 
283
  function tnp_outlook_wrapper_open($width = 600) {
284
  return NewsletterEmails::get_outlook_wrapper_open($width);
285
  }
286
+
287
  function tnp_outlook_wrapper_close() {
288
  return NewsletterEmails::get_outlook_wrapper_close();
289
  }
plugin.php CHANGED
@@ -4,7 +4,7 @@
4
  Plugin Name: Newsletter
5
  Plugin URI: https://www.thenewsletterplugin.com/plugins/newsletter
6
  Description: Newsletter is a cool plugin to create your own subscriber list, to send newsletters, to build your business. <strong>Before update give a look to <a href="https://www.thenewsletterplugin.com/category/release">this page</a> to know what's changed.</strong>
7
- Version: 6.5.5
8
  Author: Stefano Lissa & The Newsletter Team
9
  Author URI: https://www.thenewsletterplugin.com
10
  Disclaimer: Use at your own risk. No warranty expressed or implied is provided.
@@ -35,7 +35,7 @@ if (version_compare(phpversion(), '5.6', '<')) {
35
  return;
36
  }
37
 
38
- define('NEWSLETTER_VERSION', '6.5.5');
39
 
40
  global $newsletter, $wpdb;
41
 
4
  Plugin Name: Newsletter
5
  Plugin URI: https://www.thenewsletterplugin.com/plugins/newsletter
6
  Description: Newsletter is a cool plugin to create your own subscriber list, to send newsletters, to build your business. <strong>Before update give a look to <a href="https://www.thenewsletterplugin.com/category/release">this page</a> to know what's changed.</strong>
7
+ Version: 6.5.6
8
  Author: Stefano Lissa & The Newsletter Team
9
  Author URI: https://www.thenewsletterplugin.com
10
  Disclaimer: Use at your own risk. No warranty expressed or implied is provided.
35
  return;
36
  }
37
 
38
+ define('NEWSLETTER_VERSION', '6.5.6');
39
 
40
  global $newsletter, $wpdb;
41
 
readme.txt CHANGED
@@ -109,7 +109,9 @@ Thank you, The Newsletter Team
109
 
110
  == Changelog ==
111
 
112
- = NEXT =
 
 
113
 
114
  = 6.5.5 =
115
 
109
 
110
  == Changelog ==
111
 
112
+ = 6.5.6 =
113
+
114
+ * Improved post image extraction
115
 
116
  = 6.5.5 =
117