Skip to content

Commit 1427503

Browse files
committed
init
1 parent ff9a16b commit 1427503

File tree

5 files changed

+156
-0
lines changed

5 files changed

+156
-0
lines changed

.config/webpack.config.dev.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,7 @@ module.exports = [
178178
'frontend_block_horizontal_scroller': path.resolve( __dirname, '../src/block/horizontal-scroller/frontend-horizontal-scroller.js' ),
179179
'frontend_block_tabs': path.resolve( __dirname, '../src/block/tabs/frontend-tabs.js' ),
180180
'frontend_image_optimizer_polyfill': path.resolve( __dirname, '../src/block-components/image/image-optimizer-polyfill.js' ),
181+
'stk_cimo_notice': path.resolve( __dirname, '../src/compatibility/cimo/index.js' ),
181182
},
182183

183184
output: {

.config/webpack.config.prod.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,7 @@ module.exports = [
163163
'frontend_block_horizontal_scroller': path.resolve( __dirname, '../src/block/horizontal-scroller/frontend-horizontal-scroller.js' ),
164164
'frontend_block_tabs': path.resolve( __dirname, '../src/block/tabs/frontend-tabs.js' ),
165165
'frontend_image_optimizer_polyfill': path.resolve( __dirname, '../src/block-components/image/image-optimizer-polyfill.js' ),
166+
'stk_cimo_notice': path.resolve( __dirname, '../src/compatibility/cimo/index.js' ),
166167
},
167168

168169
output: {

src/compatibility/cimo/index.js

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import domReady from '@wordpress/dom-ready'
2+
class StackableCimoNotice {
3+
init = () => {
4+
// eslint-disable-next-line no-undef
5+
if ( typeof stackable === undefined || ! stackable[ 'cimo-notice' ] ||
6+
typeof wp === 'undefined' || ! wp.media || ! wp.media.view || ! wp.media.view.Attachment || ! wp.media.view.Attachment.Details
7+
) {
8+
return
9+
}
10+
11+
// eslint-disable-next-line no-undef
12+
const cimoNotice = stackable[ 'cimo-notice' ]
13+
14+
wp.media.view.Attachment.Details = wp.media.view.Attachment.Details.extend( {
15+
template: function template( view ) {
16+
const html = wp.media.template( 'attachment-details' )( view )
17+
const dom = document.createElement( 'div' )
18+
dom.innerHTML = html
19+
20+
const details = dom.querySelector( '.attachment-info' )
21+
if ( details && cimoNotice.content ) {
22+
const noticeDiv = document.createElement( 'div' )
23+
noticeDiv.className = 'stk-cimo-notice'
24+
25+
const content = document.createElement( 'p' )
26+
content.innerHTML = cimoNotice.content
27+
28+
noticeDiv.appendChild( content )
29+
details.appendChild( noticeDiv )
30+
}
31+
32+
return dom.innerHTML
33+
},
34+
} )
35+
}
36+
}
37+
38+
window.stackableCimoNotice = new StackableCimoNotice()
39+
40+
domReady( window.stackableCimoNotice.init )

src/compatibility/cimo/index.php

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
<?php
2+
/**
3+
* Cimo Notice
4+
*/
5+
6+
// Exit if accessed directly.
7+
if ( ! defined( 'ABSPATH' ) ) {
8+
exit;
9+
}
10+
11+
if ( ! class_exists( 'Stackable_Cimo_Notice' ) ) {
12+
class Stackable_Cimo_Notice {
13+
14+
private static $CIMO_SLUG = 'cimo-image-optimizer';
15+
private static $CIMO_FULL_SLUG = 'cimo-image-optimizer/cimo.php';
16+
17+
function __construct() {
18+
if ( is_admin() ) {
19+
add_action( 'enqueue_block_editor_assets', array( $this, 'enqueue_script' ), 1 );
20+
}
21+
}
22+
23+
public static function is_plugin_installed() {
24+
if ( ! function_exists( 'get_plugins' ) ) {
25+
include_once( ABSPATH . 'wp-admin/includes/plugin.php' );
26+
}
27+
28+
$all_plugins = get_plugins();
29+
if ( isset( $all_plugins[ self::$CIMO_FULL_SLUG ] ) ) {
30+
return true;
31+
}
32+
33+
return false;
34+
}
35+
36+
public static function is_plugin_activated() {
37+
if ( ! function_exists( 'is_plugin_active' ) ) {
38+
include_once( ABSPATH . 'wp-admin/includes/plugin.php' );
39+
}
40+
41+
if ( is_plugin_active( self::$CIMO_FULL_SLUG ) && defined( CIMO_FILE ) ) {
42+
return true;
43+
}
44+
45+
return false;
46+
47+
}
48+
49+
public function enqueue_script() {
50+
$cimo_state = 'activated';
51+
$cimo_action = '';
52+
53+
if ( ! self::is_plugin_installed() ) {
54+
$cimo_state = 'not_installed';
55+
$cimo_action = wp_nonce_url(
56+
add_query_arg(
57+
[
58+
'action' => 'install-plugin',
59+
'plugin' => self::$CIMO_SLUG,
60+
],
61+
admin_url( 'update.php' )
62+
),
63+
'install-plugin_' . self::$CIMO_SLUG
64+
);
65+
} else if ( ! self::is_plugin_activated() ) {
66+
$cimo_state = 'installed';
67+
$cimo_action = wp_nonce_url(
68+
admin_url( 'plugins.php?action=activate&plugin=' . self::$CIMO_FULL_SLUG ),
69+
'activate-plugin_' . self::$CIMO_FULL_SLUG
70+
);
71+
}
72+
73+
if ( $cimo_state === 'activated' && ! $cimo_action ) {
74+
return;
75+
}
76+
77+
$content = sprintf('%s <a class="stk-cimo-notice" href="%s" target="_blank">%s</a>.',
78+
__( 'Optimize your images with Cimo Image Optimizer.', STACKABLE_I18N ),
79+
$cimo_action,
80+
( $cimo_state === 'installed' ? __( 'Activate', STACKABLE_I18N ) : __( 'Install', STACKABLE_I18N ) )
81+
. ' ' . __( 'Cimo Image Optimizer', STACKABLE_I18N )
82+
);
83+
84+
$data = array(
85+
'state' => $cimo_state,
86+
'content' => $content,
87+
'action' => wp_json_encode( [ 'action' => $cimo_action ] )
88+
);
89+
90+
wp_enqueue_script(
91+
'stk-cimo-notice',
92+
plugins_url( 'dist/stk_cimo_notice.js', STACKABLE_FILE ),
93+
array(),
94+
STACKABLE_VERSION,
95+
true
96+
);
97+
98+
add_filter( 'stackable_localize_script', function ( $args ) use( $data ) {
99+
return $this->add_localize_script( $args, $data );
100+
} );
101+
102+
103+
}
104+
105+
public function add_localize_script( $args, $data ) {
106+
$args[ 'cimo-notice' ] = $data;
107+
return $args;
108+
}
109+
110+
}
111+
112+
new Stackable_Cimo_Notice();
113+
}

src/compatibility/index.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,4 @@
99
require_once( plugin_dir_path( __FILE__ ) . './ewww.php' );
1010
require_once( plugin_dir_path( __FILE__ ) . './woocommerce.php' );
1111
require_once( plugin_dir_path( __FILE__ ) . './blocksy/index.php' );
12+
require_once( plugin_dir_path( __FILE__ ) . './cimo/index.php' );

0 commit comments

Comments
 (0)