autoptimizePartners::add_partner_tabs()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 1
dl 0
loc 8
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * Handles adding "more tools" tab in AO admin settings page which promotes (future) AO
4
 * addons and/or affiliate services.
5
 */
6
7
if ( ! defined( 'ABSPATH' ) ) {
8
    exit;
9
}
10
11
class autoptimizePartners
12
{
13
    public function __construct()
14
    {
15
        $this->run();
16
    }
17
18
    public function run()
19
    {
20
        if ( $this->enabled() ) {
21
            add_filter( 'autoptimize_filter_settingsscreen_tabs', array( $this, 'add_partner_tabs' ), 10, 1 );
22
        }
23
        if ( is_multisite() && is_network_admin() && autoptimizeOptionWrapper::is_ao_active_for_network() ) {
24
            add_action( 'network_admin_menu', array( $this, 'add_admin_menu' ) );
25
        } else {
26
            add_action( 'admin_menu', array( $this, 'add_admin_menu' ) );
27
        }
28
    }
29
30
    protected function enabled()
31
    {
32
        return apply_filters( 'autoptimize_filter_show_partner_tabs', true );
33
    }
34
35
    public function add_partner_tabs( $in )
36
    {
37
        $in = array_merge( $in, array(
38
            'ao_partners' => __( 'Optimize More!', 'autoptimize' ),
39
        ) );
40
41
        return $in;
42
    }
43
44
    public function add_admin_menu()
45
    {
46
        if ( $this->enabled() ) {
47
            add_submenu_page( null, 'AO partner', 'AO partner', 'manage_options', 'ao_partners', array( $this, 'ao_partners_page' ) );
48
        }
49
    }
50
51
    protected function get_ao_partner_feed_markup()
52
    {
53
        $no_feed_text = __( 'Have a look at <a href="http://optimizingmatters.com/">optimizingmatters.com</a> for Autoptimize power-ups!', 'autoptimize' );
54
        $output       = '';
55
        if ( apply_filters( 'autoptimize_settingsscreen_remotehttp', true ) ) {
56
            $rss      = fetch_feed( 'http://feeds.feedburner.com/OptimizingMattersDownloads' );
57
            $maxitems = 0;
58
59 View Code Duplication
            if ( ! is_wp_error( $rss ) ) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
60
                $maxitems  = $rss->get_item_quantity( 20 );
61
                $rss_items = $rss->get_items( 0, $maxitems );
62
            }
63
64
            if ( 0 == $maxitems ) {
65
                $output .= $no_feed_text;
66
            } else {
67
                $output .= '<ul>';
68
                foreach ( $rss_items as $item ) {
0 ignored issues
show
Bug introduced by
The variable $rss_items does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
69
                    $item_url  = esc_url( $item->get_permalink() );
70
                    $enclosure = $item->get_enclosure();
71
72
                    $output .= '<li class="itemDetail">';
73
                    $output .= '<h3 class="itemTitle"><a href="' . $item_url . '" target="_blank">' . esc_html( $item->get_title() ) . '</a></h3>';
74
75
                    if ( $enclosure && ( false !== strpos( $enclosure->get_type(), 'image' ) ) ) {
76
                        $img_url = esc_url( $enclosure->get_link() );
77
                        $output .= '<div class="itemImage"><a href="' . $item_url . '" target="_blank"><img src="' . $img_url . '"></a></div>';
78
                    }
79
80
                    $output .= '<div class="itemDescription">' . wp_kses_post( $item->get_description() ) . '</div>';
81
                    $output .= '<div class="itemButtonRow"><div class="itemButton button-secondary"><a href="' . $item_url . '" target="_blank">' . __( 'More info', 'autoptimize' ) . '</a></div></div>';
82
                    $output .= '</li>';
83
                }
84
                $output .= '</ul>';
85
            }
86
        } else {
87
            $output .= $no_feed_text;
88
        }
89
90
        return $output;
91
    }
92
93
    public function ao_partners_page()
94
    {
95
?>
96
<style>
97
    .itemDetail {
98
        background: #fff;
99
        width: 250px;
100
        min-height: 290px;
101
        border: 1px solid #ccc;
102
        float: left;
103
        padding: 15px;
104
        position: relative;
105
        margin: 0 10px 10px 0;
106
    }
107
    .itemTitle {
108
        margin-top:0px;
109
        margin-bottom:10px;
110
    }
111
    .itemImage {
112
        text-align: center;
113
    }
114
    .itemImage img {
115
        max-width: 95%;
116
        max-height: 150px;
117
    }
118
    .itemDescription {
119
        margin-bottom:30px;
120
    }
121
    .itemButtonRow {
122
        position: absolute;
123
        bottom: 10px;
124
        right: 10px;
125
        width:100%;
126
    }
127
    .itemButton {
128
        float:right;
129
    }
130
    .itemButton a {
131
        text-decoration: none;
132
        color: #555;
133
    }
134
    .itemButton a:hover {
135
        text-decoration: none;
136
        color: #23282d;
137
    }
138
    </style>
139
    <script>document.title = "Autoptimize: <?php _e( 'Optimize More!', 'autoptimize' ); ?> " + document.title;</script>
140
    <div class="wrap">
141
        <h1><?php _e( 'Autoptimize Settings', 'autoptimize' ); ?></h1>
142
        <?php echo autoptimizeConfig::ao_admin_tabs(); ?>
143
        <?php echo '<h2>' . __( "These Autoptimize power-ups and related services will improve your site's performance even more!", 'autoptimize' ) . '</h2>'; ?>
144
        <div>
145
            <?php echo $this->get_ao_partner_feed_markup(); ?>
146
        </div>
147
    </div>
148
<?php
149
    }
150
}
151