-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patharticle-card.php
221 lines (192 loc) · 5.4 KB
/
article-card.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
<?php
/**
* @package Custom_Article_Cards
* @version 0.0.3
*/
/*
* Plugin Name: Divi 100 Article Cards
* Plugin URI: https://elegantthemes.com/
* Description: This plugin adds in <a href="http://codepen.io/andytran/pen/BNjymy">Article News Card</a> styles support for Divi Blog Post modules.
* Author: Elegant Themes
* Version: 0.0.4
* Author URI: http://elegantthemes.com/
* License: GPL3
*/
/**
* Register plugin to Divi 100 list
*/
class ET_Divi_100_Article_Card_Config
{
public static $instance;
/**
* Hook the plugin info into Divi 100 list
*/
function __construct()
{
add_filter('et_divi_100_settings', array($this, 'register'));
add_action('plugins_loaded', array($this, 'init'));
}
/**
* Gets the instance of the plugin
*/
public static function instance()
{
if (null === self::$instance) {
self::$instance = new self();
}
return self::$instance;
}
/**
* Define plugin info
*
* @return array plugin info
*/
public static function info()
{
$main_prefix = 'et_divi_100_';
$plugin_slug = 'article_cards';
return array(
'main_prefix' => $main_prefix,
'plugin_name' => __('Article Card'),
'plugin_slug' => $plugin_slug,
'plugin_id' => "{$main_prefix}{$plugin_slug}",
'plugin_prefix' => "{$main_prefix}{$plugin_slug}-",
'plugin_version' => 20160602,
'plugin_dir_path' => plugin_dir_path(__FILE__),
);
}
/**
* et_divi_100_settings callback
*
* @param array settings
* @return array settings
*/
function register($settings)
{
$info = self::info();
$settings[$info['plugin_slug']] = $info;
return $settings;
}
/**
* Init plugin after all plugins has been loaded
*/
function init()
{
// Load Divi 100 Setup
require_once(plugin_dir_path(__FILE__) . 'divi-100-setup/divi-100-setup.php');
// Load Article Card
ET_Divi_100_Article_Card::instance();
}
}
ET_Divi_100_Article_Card_Config::instance();
/**
* Load Article Cards
*/
class ET_Divi_100_Article_Card
{
/**
* Unique instance of plugin
*/
public static $instance;
public $config;
protected $settings;
protected $utils;
/**
* Gets the instance of the plugin
*/
public static function instance()
{
if (null === self::$instance) {
self::$instance = new self();
}
return self::$instance;
}
/**
* Constructor
*/
private function __construct()
{
$this->config = ET_Divi_100_Article_Card_Config::info();
$this->settings = maybe_unserialize(get_option($this->config['plugin_id']));
$this->utils = new Divi_100_Utils($this->settings);
// Initialize if Divi is active
if (et_divi_100_is_active()) {
$this->init();
}
}
/**
* Hooking methods into WordPress actions and filters
*
* @return void
*/
private function init()
{
add_action('wp_enqueue_scripts', array($this, 'enqueue_frontend_scripts'));
add_filter('body_class', array($this, 'body_class'));
if (is_admin()) {
$settings_args = array(
'plugin_id' => $this->config['plugin_id'],
'plugin_slug' => $this->config['plugin_slug'],
'title' => $this->config['plugin_name'],
'fields' => $this->settings_fields(),
);
new Divi_100_Settings($settings_args);
}
}
private function settings_fields()
{
return array(
'activate' => array(
'type' => 'toggle',
'id' => 'activate',
'label' => __('Activate Article Card'),
'description' => __('Enable this option to activate the article card style.'),
'sanitize_callback' => 'et_divi_100_sanitize_toggle',
),
'accent-color' => array(
'type' => 'color',
'id' => 'accent-color',
'label' => __('Select Accent Color'),
'description' => __('The color you choose will be used as color on category, date, and sub-title of the article card style.'),
'sanitize_callback' => 'et_divi_100_sanitize_alpha_color',
'default' => '#ffffff',
),
);
}
/**
* Load front end scripts
* @return void
*/
function enqueue_frontend_scripts()
{
wp_enqueue_style('custom-article-cards', plugin_dir_url(__FILE__) . 'assets/css/style.css', array(), $this->config['plugin_version']);
wp_enqueue_script('custom-article-cards', plugin_dir_url(__FILE__) . 'assets/js/scripts.js', array('jquery', 'divi-custom-script'), $this->config['plugin_version'], true);
// Add custom css
$settings = $this->settings_fields();
$accent_color_default = $settings['accent-color']['default'];
$accent_color = $this->utils->get_value('accent-color', $accent_color_default);
if ($accent_color && $accent_color !== $accent_color_default) {
$custom_css = sprintf(
'.divi-100-article-card .et_pb_blog_grid .article-card__category,
.divi-100-article-card .et_pb_blog_grid .article-card__date { background-color: %1$s; }
.divi-100-article-card .et_pb_blog_grid .article-card__sub-title { color: %1$s; }',
et_divi_100_sanitize_alpha_color($accent_color)
);
wp_add_inline_style('custom-article-cards', $custom_css);
}
}
/**
* Add specific class to <body>
* @return array
*/
function body_class($classes)
{
// Get selected style
$activation = et_divi_100_sanitize_toggle($this->utils->get_value('activate'));
// Assign specific class to <body> if needed
if (!is_admin() && 'on' === $activation) {
$classes[] = esc_attr('divi-100-article-card');
}
return $classes;
}
}