/var/www/html_us/wp-content/plugins/checkout-for-woocommerce/includes/Features/PhpSnippets.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
<?php

namespace Objectiv\Plugins\Checkout\Features;

use 
Objectiv\Plugins\Checkout\Admin\Pages\PageAbstract;
use 
Objectiv\Plugins\Checkout\Interfaces\SettingsGetterInterface;
use 
Objectiv\Plugins\Checkout\Managers\SettingsManager;

class 
PhpSnippets extends FeaturesAbstract {
    protected 
$php_snippets_field_name;

    public function 
__constructbool $enabledbool $availablestring $required_plans_listSettingsGetterInterface $settings_getterstring $php_snippets_field_name ) {
        
$this->php_snippets_field_name $php_snippets_field_name;

        
parent::__construct$enabled$available$required_plans_list$settings_getter );
    }

    protected function 
run_if_cfw_is_enabled() {
        
$php_snippets $this->sanitize_snippet$this->settings_getter->get_setting'php_snippets' ) );

        if ( empty( 
$php_snippets ) ) {
            return;
        }

        if ( 
class_exists'\\ParseError' ) ) {
            try {
                eval( 
$php_snippets ); // phpcs:ignore
            
} catch( \ParseError $e ) { // phpcs:ignore
                
error_log'CheckoutWC: Failed to load PHP snippets. Parse Error: ' $e->getMessage() );
            }
        } else {
            eval( 
$php_snippets ); // phpcs:ignore
        
}
    }

    
/**
     * @param string $code
     * @return string
     */
    
private function sanitize_snippetstring $code ): string {
        
/* Remove <?php and <? from beginning of snippet */
        
$code preg_replace'|^[\s]*<\?(php)?|'''$code );

        
/* Remove ?> from end of snippet */
        
$code preg_replace'|\?>[\s]*$|'''$code );

        return 
strval$code );
    }

    public function 
init() {
        
parent::init();

        
add_action'cfw_do_plugin_activation', array( $this'run_on_plugin_activation' ) );
        
add_action'cfw_advanced_scripts_after_admin_page_controls', array( $this'output_admin_settings' ) );
    }

    
/**
     * @param PageAbstract $advanced_admin_page
     */
    
public function output_admin_settingsPageAbstract $advanced_admin_page ) {
        if ( ! 
$this->available ) {
            
$notice $advanced_admin_page->get_upgrade_required_notice$this->required_plans_list );
        }

        
$advanced_admin_page->output_textarea_row(
            
'php_snippets',
            
cfw__'PHP Snippets''checkout-wc' ),
            
sprintfcfw__'Add PHP snippets to modify your checkout page here. If you have lots of snippets, you may want to consider using <a target="_blank" href="%s">Code Snippets</a>.''checkout-wc' ), 'https://wordpress.org/plugins/code-snippets/' ),
            
$this->available,
            
10,
            
$notice ?? ''
        
);
    }

    public function 
run_on_plugin_activation() {
        
SettingsManager::instance()->add_setting'php_snippets''' );
    }
}