fbcomment_plugin.php ➔ fbcom_plugin()   D
last analyzed

Complexity

Conditions 15
Paths 224

Size

Total Lines 75
Code Lines 45

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 15
eloc 45
nc 224
nop 2
dl 0
loc 75
rs 4.4825
c 0
b 0
f 0

How to fix   Long Method    Complexity   

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
 * include/fbcomment_plugin.php - supply gwiki meta data open graph style for fbcomment module
4
 *
5
 * This file is part of gwiki - geekwright wiki
6
 *
7
 * @copyright  Copyright © 2013 geekwright, LLC. All rights reserved.
8
 * @license    gwiki/docs/license.txt  GNU General Public License (GPL)
9
 * @since      1.0
10
 * @author     Richard Griffith <[email protected]>
11
 * @package    gwiki
12
 */
13
// defined('XOOPS_ROOT_PATH') || exit('XOOPS root path not defined');
14
/*
15
 * Open Graph Meta Tags we can set here:
16
 *
17
 *   $metas['fb:admins'] = $admins;
18
 *   $metas['fb:app_id'] = $appid;
19
 *   $metas['og:type']=$type;
20
 *   $metas['og:url']=$oururl;
21
 *   $metas['og:title']=$title;
22
 *   $metas['og:description']=$description;
23
 *   $metas['og:image']=$image;
24
 *   $metas['og:site_name'] = $sitename;
25
 *
26
 */
27
28
/**
29
 * @param $metas
30
 * @param $plugin_env
31
 *
32
 * @return bool
33
 */
34
function fbcom_plugin(&$metas, $plugin_env)
35
{
36
    global $xoopsDB;
37
38
    $dir = basename(dirname(__DIR__));
39
    $moduleHelper = Xmf\Module\Helper::getHelper($dir);
40
41
    $wikihome = strtolower($moduleHelper->getConfig('wiki_home_page'));
42
43
    // fake a full url with page if at top of module
44
    if (!isset($plugin_env['page']) && substr($metas['og:url'], -1) === '/') {
45
        $plugin_env['page'] = $wikihome;
46
        $metas['og:url'] .= 'index.php'; //$metas['og:url']    = $metas['og:url'] . 'index.php';
47
    }
48
49
    if (isset($plugin_env['page'])) {
50
        // cononicalize our url with our rules
51
        // - page needs to be case insensitve (AbCde and AbcDe yield the same page)
52
        $keyword = strtolower($plugin_env['page']);
53
        // - strip any OOB data
54
        if (substr($keyword, -1) === ')') {
55
            $lparen = strpos($keyword, '(');
56
            if ($lparen !== false) {
57
                $keyword = substr($keyword, 0, $lparen);
58
            }
59
        }
60
61
        // - eliminate index.php?page=wikihome
62
        $ourscript       = explode('?', urldecode($metas['og:url']));
63
        $ourscript_parts = pathinfo($ourscript[0]);
64
        if ($ourscript_parts['basename'] !== 'index.php') {
65
            return false;
66
        }
67
        if ($keyword === $wikihome && $ourscript_parts['basename'] === 'index.php') {
68
            $newscript = $ourscript_parts['dirname'] . '/';
69
        } else {
70
            $newscript = $ourscript[0] . '?page=' . $keyword;
71
        }
72
        $metas['og:url'] = $newscript;
73
74
        $wikitable  = $xoopsDB->prefix('gwiki_pages');
75
        $imagetable = $xoopsDB->prefix('gwiki_page_images');
76
        $sql        = "SELECT title, meta_description, search_body, image_file FROM {$wikitable} p ";
77
        $sql .= " left join {$imagetable} i on p.keyword=i.keyword and use_to_represent = 1 ";
78
        $sql .= " where p.keyword = '{$keyword}' and active=1 ";
79
80
        // set title and description
81
        $result = $xoopsDB->query($sql);
82
        if ($result) {
83
            //          if(!$xoopsDB->getRowsNum($result)) return false;
84
            if ($myrow = $xoopsDB->fetchArray($result)) {
85
                if (!empty($myrow['title'])) {
86
                    $metas['og:title'] = $myrow['title'];
87
                }
88
                if (!empty($myrow['search_body'])) {
89
                    $description             = $myrow['search_body'];
90
                    $description             = substr($description, 0, 40) . '...';
91
                    $metas['og:description'] = $description;
92
                }
93
                if (!empty($myrow['meta_description'])) {
94
                    $description             = $myrow['meta_description'];
95
                    $metas['og:description'] = $description;
96
                }
97
                if (!empty($myrow['image_file'])) {
98
                    $image_file        = $myrow['image_file'];
99
                    $metas['og:image'] = XOOPS_URL . '/uploads/' . $dir . '/' . $image_file;
100
                }
101
102
                return true;
103
            }
104
        }
105
    }
106
107
    return false;
108
}
109