Completed
Branch dev (a3767f)
by Michael
40:38
created

getContentsByHeader()   B

Complexity

Conditions 6
Paths 10

Size

Total Lines 32
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 42
Metric Value
dl 0
loc 32
rs 8.439
ccs 0
cts 26
cp 0
cc 6
eloc 19
nc 10
nop 2
crap 42
1
<?php
2
/**
3
 * Admin Page Framework Loader
4
 * 
5
 * Demonstrates the usage of Admin Page Framework.
6
 * 
7
 * http://en.michaeluno.jp/admin-page-framework/
8
 * Copyright (c) 2013-2015 Michael Uno; Licensed GPLv2
9
 * 
10
 */
11
12
/**
13
 * A base class that provides methods to display readme file contents.
14
 * 
15
 * @sicne       3.5.3       Extends `AdminPageFrameworkLoader_AdminPage_Tab_Base`.
16
 * @extends     AdminPageFrameworkLoader_AdminPage_Tab_Base
17
 */
18
abstract class AdminPageFrameworkLoader_AdminPage_Tab_ReadMeBase extends AdminPageFrameworkLoader_AdminPage_Tab_Base {
2 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
Coding Style introduced by
As per PSR2, the opening brace for this class should be on a new line.
Loading history...
19
        
20
    /**
21
     * 
22
     * @since       3.5.3
23
     */
24
    protected function _getReadmeContents( $sFilePath, $sTOCTitle, $asSections=array() ) {
1 ignored issue
show
Coding Style introduced by
Method name "_getReadmeContents" should not be prefixed with an underscore to indicate visibility
Loading history...
25
        
26
        $_oWPReadmeParser = new AdminPageFramework_WPReadmeParser( 
27
            $sFilePath, 
28
            array( // replacements
29
                '%PLUGIN_DIR_URL%'  => AdminPageFrameworkLoader_Registry::getPluginURL(),
30
                '%WP_ADMIN_URL%'    => admin_url(),
31
            ),
32
            array( // callbacks
33
                'content_before_parsing' => array( $this, '_replyToProcessShortcodes' ),
34
            )
35
        );    
36
        $_sContent = '';
37
        foreach( ( array ) $asSections as $_sSection  ) {
38
            $_sContent .= $_oWPReadmeParser->getSection( $_sSection );  
39
        }        
40
        if ( $sTOCTitle ) {            
41
            $_oTOC = new AdminPageFramework_TableOfContents(
42
                $_sContent,
43
                4,
44
                $sTOCTitle
45
            );
46
            return $_oTOC->get();        
47
        }
48
        return ''
49
         . $_sContent;
50
        
51
    }
52
    
53
        /**
54
         * @return      string 
55
         * @return      3.6.1
0 ignored issues
show
Documentation introduced by
The doc-type 3.6.1 could not be parsed: Unknown type name "3.6.1" at position 0. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
56
         */
57
        public function _replyToProcessShortcodes( $sContent ) {
1 ignored issue
show
Coding Style introduced by
Method name "_replyToProcessShortcodes" should not be prefixed with an underscore to indicate visibility
Loading history...
58
59
            // Register the 'embed' shortcode.
60
            add_shortcode( 'embed', array( $this, '_replyToProcessShortcode_embed' ) );
61
            return do_shortcode( $sContent );
62
            
63
        }
64
               
65
            /**
66
             * @since       3.6.1
67
             * @return      string      The generate HTML output.
68
             */
69
            public function _replyToProcessShortcode_embed( $aAttributes, $sURL, $sShortcode='' ) {
1 ignored issue
show
Unused Code introduced by
The parameter $sShortcode is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Coding Style introduced by
Method name "_replyToProcessShortcode_embed" should not be prefixed with an underscore to indicate visibility
Loading history...
70
71
                $sURL   = isset( $aAttributes[ 'src' ] ) ? $aAttributes[ 'src' ] : $sURL;      
72
                $_sHTML = wp_oembed_get( $sURL );
73
                
74
                // If there was a result, return it
75
                if ( $_sHTML ) {
76
                    // This filter is documented in wp-includes/class-wp-embed.php
77
                    return "<div class='video oembed'>" 
78
                                . apply_filters(
79
                                    'embed_oembed_html', 
80
                                    $_sHTML, 
81
                                    $sURL, 
82
                                    $aAttributes, 
83
                                    0
84
                                )
85
                        . "</div>";
86
                }        
87
                
88
                // If not found, return the link.
89
                $_oWPEmbed = new WP_Embed;        
90
                return "<div class='video oembed'>" 
91
                        . $_oWPEmbed->maybe_make_link( $sURL )
92
                    . "</div>";
93
                
94
            }         
95
 
96
    /**
97
     * Returns HTML contents divided by heading.
98
     * 
99
     * For example,
100
     * <h3>First Heading</h3>
101
     * Some text.
102
     * <h3>Second Heading</h3>
103
     * Another text.
104
     * 
105
     * Will be
106
     * array(  
107
     *  array( 'First Heading' => 'Some text', ),
108
     *  array( 'Second Heading' => 'Another text', ),
109
     * )
110
     */
111
    public function getContentsByHeader( $sContents, $iHeaderNumber=2 ) {
112
    
113
        $_aContents = array();
114
        $_aSplitContents = preg_split( 
115
            // '/^[\s]*==[\s]*(.+?)[\s]*==/m', 
116
            '/(<h[' . $iHeaderNumber . ']*[^>]*>.*?<\/h[' . $iHeaderNumber . ']>)/i',
117
            $sContents,
118
            -1, 
119
            PREG_SPLIT_DELIM_CAPTURE|PREG_SPLIT_NO_EMPTY 
120
        );                   
121
122
        foreach( $_aSplitContents as $_iIndex => $_sSplitContent ) {
123
            if ( ! preg_match( '/<h[' . $iHeaderNumber . ']*[^>]*>(.*?)<\/h[' . $iHeaderNumber . ']>/i', $_sSplitContent , $_aMatches ) ) {
124
                continue;
125
            }
126
        
127
            if ( ! isset( $_aMatches[ 1 ] ) ) {
128
                continue;
129
            }
130
            if ( isset( $_aSplitContents[ $_iIndex + 1 ] ) )  {
131
                $_aContents[] = array( 
132
                    $_aMatches[ 1 ],
133
                    $_aSplitContents[ $_iIndex + 1 ]
134
                );
135
            }
136
        }
137
   
138
        return empty( $_aContents )
139
            ? array( array( '', $sContents ) ) 
140
            : $_aContents;
141
        
142
    }
143
 
144
}
1 ignored issue
show
Coding Style introduced by
According to PSR2, the closing brace of classes should be placed on the next line directly after the body.

Below you find some examples:

// Incorrect placement according to PSR2
class MyClass
{
    public function foo()
    {

    }
    // This blank line is not allowed.

}

// Correct
class MyClass
{
    public function foo()
    {

    } // No blank lines after this line.
}
Loading history...