AdminPageFrameworkLoader_AdminPage_Tab_ReadMeBase   A
last analyzed

Complexity

Total Complexity 13

Size/Duplication

Total Lines 123
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 13
eloc 51
dl 0
loc 123
rs 10
c 0
b 0
f 0
ccs 0
cts 73
cp 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getContentsByHeader() 0 30 6
A _replyToProcessShortcodes() 0 5 1
A _replyToProcessShortcode_embed() 0 24 3
A _getReadmeContents() 0 26 3
1
<?php
2
/**
3
 * Admin Page Framework Loader
4
 *
5
 * Demonstrates the usage of Admin Page Framework.
6
 *
7
 * http://admin-page-framework.michaeluno.jp/
8
 * Copyright (c) 2013-2022, 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 {
19
20
    /**
21
     *
22
     * @since       3.5.3
23
     */
24
    protected function _getReadmeContents( $sFilePath, $sTOCTitle, $asSections=array() ) {
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
56
         */
57
        public function _replyToProcessShortcodes( $sContent ) {
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='' ) {
0 ignored issues
show
Unused Code introduced by
The parameter $sShortcode is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

69
            public function _replyToProcessShortcode_embed( $aAttributes, $sURL, /** @scrutinizer ignore-unused */ $sShortcode='' ) {

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

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 )
0 ignored issues
show
Bug introduced by
Are you sure $_oWPEmbed->maybe_make_link($sURL) of type false|string can be used in concatenation? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

91
                        . /** @scrutinizer ignore-type */ $_oWPEmbed->maybe_make_link( $sURL )
Loading history...
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
}
145