Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
W
WP Mail
Manage
Activity
Members
Labels
Plan
Issues
0
Issue boards
Milestones
Wiki
Code
Merge requests
0
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package Registry
Container Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
wpdesk
Library
WP Mail
Commits
b537f22a
Commit
b537f22a
authored
10 months ago
by
Piotr Potrebka
Browse files
Options
Downloads
Patches
Plain Diff
feat: refactor
parent
bf1b3c6f
No related branches found
No related tags found
2 merge requests
!5
Devel
,
!4
Refactor/templates
Pipeline
#406283
passed with warnings with stages
in 30 seconds
Changes
4
Pipelines
1
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
src/Abstracts/Email.php
+10
-1
10 additions, 1 deletion
src/Abstracts/Email.php
src/Helpers/StyleInliner.php
+0
-41
0 additions, 41 deletions
src/Helpers/StyleInliner.php
src/Template.php
+32
-3
32 additions, 3 deletions
src/Template.php
src/WPMailer.php
+15
-2
15 additions, 2 deletions
src/WPMailer.php
with
57 additions
and
47 deletions
src/Abstracts/Email.php
+
10
−
1
View file @
b537f22a
...
...
@@ -164,7 +164,7 @@ class Email {
/**
* @return string
*/
public
function
set_content_type
(
$type
=
'html'
):
self
{
public
function
set_content_type
(
$type
):
self
{
switch
(
$type
)
{
case
'plain'
:
$content_type
=
'text/plain'
;
...
...
@@ -200,12 +200,21 @@ class Email {
return
$this
->
content
;
}
/**
* @param string $name
* @param string $value
*
* @return $this
*/
public
function
set_template_attributes
(
string
$name
,
string
$value
):
self
{
$this
->
template_attributes
[
$name
]
=
$value
;
return
$this
;
}
/**
* @return array
*/
public
function
get_template_attributes
():
array
{
return
$this
->
template_attributes
;
}
...
...
This diff is collapsed.
Click to expand it.
src/Helpers/StyleInliner.php
deleted
100644 → 0
+
0
−
41
View file @
bf1b3c6f
<?php
namespace
WPDesk\Library\WPEmail\Helpers
;
use
Pelago\Emogrifier\CssInliner
;
use
Pelago\Emogrifier\HtmlProcessor\CssToAttributeConverter
;
use
Pelago\Emogrifier\HtmlProcessor\HtmlPruner
;
class
StyleInliner
{
public
static
function
inline
(
string
$content
,
string
$styles
=
''
):
string
{
if
(
class_exists
(
'DOMDocument'
)
)
{
try
{
$css_inliner
=
CssInliner
::
fromHtml
(
$content
)
->
inlineCss
(
$styles
);
$dom_document
=
$css_inliner
->
getDomDocument
();
HtmlPruner
::
fromDomDocument
(
$dom_document
)
->
removeElementsWithDisplayNone
();
$content
=
CssToAttributeConverter
::
fromDomDocument
(
$dom_document
)
->
convertCssToVisualAttributes
()
->
render
();
}
catch
(
\Exception
$e
)
{
error_log
(
$e
->
getMessage
()
);
}
}
else
{
$content
=
'<style>'
.
strip_tags
(
$styles
)
.
'</style>'
.
$content
;
}
return
$content
;
}
/**
* @return array|string|string[]
*/
protected
function
replace_placeholders
(
string
$string
):
string
{
if
(
empty
(
$this
->
placeholders
)
)
{
return
$string
;
}
return
(
string
)
str_replace
(
array_keys
(
$this
->
placeholders
),
array_values
(
$this
->
placeholders
),
$string
);
}
}
This diff is collapsed.
Click to expand it.
src/Template.php
+
32
−
3
View file @
b537f22a
...
...
@@ -2,11 +2,20 @@
namespace
WPDesk\Library\WPEmail
;
use
WPDesk\Library\WPEmail\Helpers\StyleInliner
;
use
Exception
;
use
Pelago\Emogrifier\CssInliner
;
use
Pelago\Emogrifier\HtmlProcessor\CssToAttributeConverter
;
use
Pelago\Emogrifier\HtmlProcessor\HtmlPruner
;
use
Psr\Log\LoggerInterface
;
use
WPDesk\View\Renderer\Renderer
;
class
Template
{
/**
* @var LoggerInterface
*/
private
$logger
;
/**
* @var Renderer
*/
...
...
@@ -17,7 +26,8 @@ class Template {
*/
private
$template_attributes
;
public
function
__construct
(
Renderer
$renderer
,
array
$template_attributes
)
{
public
function
__construct
(
LoggerInterface
$logger
,
Renderer
$renderer
,
array
$template_attributes
)
{
$this
->
logger
=
$logger
;
$this
->
renderer
=
$renderer
;
$this
->
template_attributes
=
wp_parse_args
(
$template_attributes
,
$this
->
get_default_template_attributes
()
);
}
...
...
@@ -38,7 +48,7 @@ class Template {
public
function
css_inline
(
string
$content
):
string
{
$styles
=
$this
->
renderer
->
render
(
'html/email-styles'
,
$this
->
template_attributes
);
return
StyleInliner
::
inline
(
$content
,
$styles
);
return
$this
->
convert_css
(
$content
,
$styles
);
}
public
function
get_default_template_attributes
():
array
{
...
...
@@ -53,4 +63,23 @@ class Template {
];
}
public
function
convert_css
(
string
$content
,
string
$styles
=
''
):
string
{
if
(
class_exists
(
'DOMDocument'
)
)
{
try
{
$css_inliner
=
CssInliner
::
fromHtml
(
$content
)
->
inlineCss
(
$styles
);
$dom_document
=
$css_inliner
->
getDomDocument
();
HtmlPruner
::
fromDomDocument
(
$dom_document
)
->
removeElementsWithDisplayNone
();
$content
=
CssToAttributeConverter
::
fromDomDocument
(
$dom_document
)
->
convertCssToVisualAttributes
()
->
render
();
}
catch
(
Exception
$e
)
{
$this
->
logger
->
debug
(
$e
->
getMessage
()
);
return
$content
;
}
}
else
{
$content
=
'<style>'
.
strip_tags
(
$styles
)
.
'</style>'
.
$content
;
}
return
$content
;
}
}
This diff is collapsed.
Click to expand it.
src/WPMailer.php
+
15
−
2
View file @
b537f22a
...
...
@@ -3,6 +3,8 @@
namespace
WPDesk\Library\WPEmail
;
use
Exception
;
use
Psr\Log\LoggerAwareInterface
;
use
Psr\Log\LoggerInterface
;
use
WP_Error
;
use
WPDesk\Library\WPEmail\Abstracts\Email
;
use
WPDesk\Library\WPEmail\Abstracts\Mailer
;
...
...
@@ -14,12 +16,22 @@ use WPDesk\View\Resolver\DirResolver;
class
WPMailer
implements
Mailer
{
/**
* @var LoggerInterface
*/
private
$logger
;
/**
* @var Renderer
*/
private
$renderer
;
public
function
__construct
()
{
public
function
__construct
(
LoggerInterface
$logger
)
{
$this
->
logger
=
$logger
;
$this
->
init_renderer
();
}
public
function
init_renderer
()
{
$resolver
=
new
ChainResolver
();
$resolver
->
appendResolver
(
new
DirResolver
(
__DIR__
.
'/templates'
)
);
$renderer
=
new
SimplePhpRenderer
(
$resolver
);
...
...
@@ -53,7 +65,7 @@ class WPMailer implements Mailer {
);
add_action
(
'wp_mail_failed'
,
[
$this
,
'catch_error'
]
);
$email_template
=
new
Template
(
$this
->
renderer
,
$email
->
get_template_attributes
()
);
$email_template
=
new
Template
(
$this
->
logger
,
$this
->
get_
renderer
()
,
$email
->
get_template_attributes
()
);
try
{
$success
=
wp_mail
(
...
...
@@ -84,4 +96,5 @@ class WPMailer implements Mailer {
throw
MailerException
::
with_wp_error
(
$error
);
}
}
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment