Completed
Push — master ( 8e7dca...6ec0d0 )
by frank
01:41
created

autoptimizePartners::ao_partners_page()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 56

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 56
rs 8.9599
c 0
b 0
f 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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
        add_action( 'admin_menu', array( $this, 'add_admin_menu' ) );
24
    }
25
26
    protected function enabled()
27
    {
28
        return apply_filters( 'autoptimize_filter_show_partner_tabs', true );
29
    }
30
31
    public function add_partner_tabs( $in )
32
    {
33
        $in = array_merge( $in, array(
34
            'ao_partners' => __( 'Optimize More!', 'autoptimize' ),
35
        ) );
36
37
        return $in;
38
    }
39
40
    public function add_admin_menu()
41
    {
42
        if ( $this->enabled() ) {
43
            add_submenu_page( null, 'AO partner', 'AO partner', 'manage_options', 'ao_partners', array( $this, 'ao_partners_page' ) );
44
        }
45
    }
46
47
    protected function get_ao_partner_feed_markup()
48
    {
49
        $no_feed_text = __( 'Have a look at <a href="http://optimizingmatters.com/">optimizingmatters.com</a> for Autoptimize power-ups!', 'autoptimize' );
50
        $output       = '';
51
        if ( apply_filters( 'autoptimize_settingsscreen_remotehttp', true ) ) {
52
            $rss      = fetch_feed( 'http://feeds.feedburner.com/OptimizingMattersDownloads' );
53
            $maxitems = 0;
54
55 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...
56
                $maxitems  = $rss->get_item_quantity( 20 );
57
                $rss_items = $rss->get_items( 0, $maxitems );
58
            }
59
60
            if ( 0 == $maxitems ) {
61
                $output .= $no_feed_text;
62
            } else {
63
                $output .= '<ul>';
64
                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...
65
                    $item_url  = esc_url( $item->get_permalink() );
66
                    $enclosure = $item->get_enclosure();
67
68
                    $output .= '<li class="itemDetail">';
69
                    $output .= '<h3 class="itemTitle"><a href="' . $item_url . '" target="_blank">' . esc_html( $item->get_title() ) . '</a></h3>';
70
71
                    if ( $enclosure && ( false !== strpos( $enclosure->get_type(), 'image' ) ) ) {
72
                        $img_url = esc_url( $enclosure->get_link() );
73
                        $output .= '<div class="itemImage"><a href="' . $item_url . '" target="_blank"><img src="' . $img_url . '"></a></div>';
74
                    }
75
76
                    $output .= '<div class="itemDescription">' . wp_kses_post( $item->get_description() ) . '</div>';
77
                    $output .= '<div class="itemButtonRow"><div class="itemButton button-secondary"><a href="' . $item_url . '" target="_blank">' . __( 'More info', 'autoptimize' ) . '</a></div></div>';
78
                    $output .= '</li>';
79
                }
80
                $output .= '</ul>';
81
            }
82
        } else {
83
            $output .= $no_feed_text;
84
        }
85
86
        return $output;
87
    }
88
89
    public function ao_partners_page()
90
    {
91
?>
92
<style>
93
    .itemDetail {
94
        background: #fff;
95
        width: 250px;
96
        min-height: 290px;
97
        border: 1px solid #ccc;
98
        float: left;
99
        padding: 15px;
100
        position: relative;
101
        margin: 0 10px 10px 0;
102
    }
103
    .itemTitle {
104
        margin-top:0px;
105
        margin-bottom:10px;
106
    }
107
    .itemImage {
108
        text-align: center;
109
    }
110
    .itemImage img {
111
        max-width: 95%;
112
        max-height: 150px;
113
    }
114
    .itemDescription {
115
        margin-bottom:30px;
116
    }
117
    .itemButtonRow {
118
        position: absolute;
119
        bottom: 10px;
120
        right: 10px;
121
        width:100%;
122
    }
123
    .itemButton {
124
        float:right;
125
    }
126
    .itemButton a {
127
        text-decoration: none;
128
        color: #555;
129
    }
130
    .itemButton a:hover {
131
        text-decoration: none;
132
        color: #23282d;
133
    }
134
    </style>
135
    <div class="wrap">
136
        <h1><?php _e( 'Autoptimize Settings', 'autoptimize' ); ?></h1>
137
        <?php echo autoptimizeConfig::ao_admin_tabs(); ?>
138
        <?php echo '<h2>' . __( "These Autoptimize power-ups and related services will improve your site's performance even more!", 'autoptimize' ) . '</h2>'; ?>
139
        <div>
140
            <?php echo $this->get_ao_partner_feed_markup(); ?>
141
        </div>
142
    </div>
143
<?php
144
    }
145
}
146