-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclass-frontend.php
More file actions
154 lines (127 loc) · 4.87 KB
/
Copy pathclass-frontend.php
File metadata and controls
154 lines (127 loc) · 4.87 KB
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
<?php
defined( 'ABSPATH' ) || exit;
class Filecheck_Frontend {
protected static $_instance = null;
public static function instance() {
if ( is_null( self::$_instance ) ) {
self::$_instance = new self();
}
return self::$_instance;
}
public function __construct() {
add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_frontend_assets' ) );
add_action( 'woocommerce_before_add_to_cart_button', array( $this, 'render_filecheck_element' ) );
add_filter( 'woocommerce_loop_add_to_cart_link', array( $this, 'modify_loop_add_to_cart_link' ), 10, 2 );
}
/**
* Replaces the direct "Add to Cart" button on shop/catalog pages with a link to the product details page
* for any products that require Filecheck.
*/
public function modify_loop_add_to_cart_link( $html, $product ) {
$workflow_id = get_post_meta( $product->get_id(), '_filecheck_workflow_id', true );
if ( ! empty( $workflow_id ) && 'none' !== $workflow_id ) {
$html = sprintf(
'<a href="%s" class="button">%s</a>',
esc_url( $product->get_permalink() ),
esc_html__( 'Read more', 'filecheck' )
);
}
return $html;
}
public function render_filecheck_element() {
$product = wc_get_product( get_queried_object_id() );
if ( ! $product instanceof WC_Product ) {
return;
}
$product_id = $product->get_id();
$workflow_id = get_post_meta( $product_id, '_filecheck_workflow_id', true );
if ( empty( $workflow_id ) ) {
$workflow_id = 'none';
}
if ( 'none' === $workflow_id ) {
return;
}
if ( 'global' === $workflow_id ) {
$workflow_id = get_option( 'filecheck_default_workflow_id' );
}
if ( empty( $workflow_id ) ) {
if ( current_user_can( 'manage_woocommerce' ) ) {
echo '<div style="color:red; margin: 10px 0;">' . esc_html__( 'Filecheck error: Global default workflow is not configured.', 'filecheck' ) . '</div>';
}
return;
}
?>
<div id="fc-slot-<?php echo esc_attr( $product_id ); ?>"
class="fc-slot-wrapper"
data-product-id="<?php echo esc_attr( $product_id ); ?>"
data-workflow-id="<?php echo esc_attr( $workflow_id ); ?>"></div>
<input type="hidden" name="filecheck_job_id" id="fc-jobid" value="">
<?php
}
public function enqueue_frontend_assets() {
// Cart page only needs the stylesheet (proof thumbnails).
if ( is_cart() ) {
wp_enqueue_style(
'filecheck-frontend',
FILECHECK_PLUGIN_URL . 'assets/css/frontend.css',
array(),
FILECHECK_VERSION
);
return;
}
if ( ! is_product() ) {
return;
}
$product = wc_get_product( get_queried_object_id() );
if ( ! $product instanceof WC_Product ) {
return;
}
$product_id = $product->get_id();
$workflow_id = get_post_meta( $product_id, '_filecheck_workflow_id', true );
if ( empty( $workflow_id ) ) {
$workflow_id = 'none';
}
if ( 'none' === $workflow_id ) {
return;
}
$pk = get_option( 'filecheck_publishable_key' );
if ( empty( $pk ) ) {
return;
}
if ( 'global' === $workflow_id ) {
$workflow_id = get_option( 'filecheck_default_workflow_id' );
}
$agent_id = get_option( 'filecheck_agent_id' );
// Enqueue Filecheck CDN Element script (pk-specific URL embeds tenant config)
wp_enqueue_script(
'filecheck-cdn-element',
'https://cdn.filecheck.io/element/' . rawurlencode( $pk ) . '/filecheck.js',
array(),
null,
false
);
wp_enqueue_script(
'filecheck-frontend',
FILECHECK_PLUGIN_URL . 'assets/js/frontend.js',
array(),
FILECHECK_VERSION,
true
);
$connector_id = get_post_meta( $product_id, '_filecheck_connector_id', true );
wp_localize_script( 'filecheck-frontend', 'filecheck_params', array(
'publishable_key' => $pk,
'agent_id' => $agent_id,
'workflow_id' => $workflow_id,
'connector_id' => $connector_id,
'product_id' => $product_id,
'ajax_url' => admin_url( 'admin-ajax.php' ),
'nonce' => wp_create_nonce( 'filecheck_save_job' ),
) );
wp_enqueue_style(
'filecheck-frontend',
FILECHECK_PLUGIN_URL . 'assets/css/frontend.css',
array(),
FILECHECK_VERSION
);
}
}