Completed
Push — master ( ad6325...4bb5f3 )
by Michael
02:50
created

fbcomment_plugin.php ➔ fbcom_plugin()   D

Complexity

Conditions 15
Paths 224

Size

Total Lines 79
Code Lines 48

Duplication

Lines 0
Ratio 0 %

Importance

Changes 8
Bugs 0 Features 1
Metric Value
cc 15
eloc 48
c 8
b 0
f 1
nc 224
nop 2
dl 0
loc 79
rs 4.3221

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
    // Access module configs from block:
40
    $moduleHandler = xoops_getHandler('module');
41
    $module        = $moduleHandler->getByDirname($dir);
42
    $configHandler = xoops_getHandler('config');
43
    $moduleConfig  = $configHandler->getConfigsByCat(0, $module->getVar('mid'));
44
45
    $wikihome = strtolower($moduleConfig['wiki_home_page']);
46
47
    // fake a full url with page if at top of module
48
    if (!isset($plugin_env['page']) && substr($metas['og:url'], -1) === '/') {
49
        $plugin_env['page'] = $wikihome;
50
        $metas['og:url'] .= 'index.php'; //$metas['og:url']    = $metas['og:url'] . 'index.php';
51
    }
52
53
    if (isset($plugin_env['page'])) {
54
        // cononicalize our url with our rules
55
        // - page needs to be case insensitve (AbCde and AbcDe yield the same page)
56
        $keyword = strtolower($plugin_env['page']);
57
        // - strip any OOB data
58
        if (substr($keyword, -1) === ')') {
59
            $lparen = strpos($keyword, '(');
60
            if ($lparen !== false) {
61
                $keyword = substr($keyword, 0, $lparen);
62
            }
63
        }
64
65
        // - eliminate index.php?page=wikihome
66
        $ourscript       = explode('?', urldecode($metas['og:url']));
67
        $ourscript_parts = pathinfo($ourscript[0]);
68
        if ($ourscript_parts['basename'] !== 'index.php') {
69
            return false;
70
        }
71
        if ($keyword === $wikihome && $ourscript_parts['basename'] === 'index.php') {
72
            $newscript = $ourscript_parts['dirname'] . '/';
73
        } else {
74
            $newscript = $ourscript[0] . '?page=' . $keyword;
75
        }
76
        $metas['og:url'] = $newscript;
77
78
        $wikitable  = $xoopsDB->prefix('gwiki_pages');
79
        $imagetable = $xoopsDB->prefix('gwiki_page_images');
80
        $sql        = "SELECT title, meta_description, search_body, image_file FROM {$wikitable} p ";
81
        $sql .= " left join {$imagetable} i on p.keyword=i.keyword and use_to_represent = 1 ";
82
        $sql .= " where p.keyword = '{$keyword}' and active=1 ";
83
84
        // set title and description
85
        $result = $xoopsDB->query($sql);
86
        if ($result) {
87
            //          if(!$xoopsDB->getRowsNum($result)) return false;
88
            if ($myrow = $xoopsDB->fetchArray($result)) {
89
                if (!empty($myrow['title'])) {
90
                    $metas['og:title'] = $myrow['title'];
91
                }
92
                if (!empty($myrow['search_body'])) {
93
                    $description             = $myrow['search_body'];
94
                    $description             = substr($description, 0, 40) . '...';
95
                    $metas['og:description'] = $description;
96
                }
97
                if (!empty($myrow['meta_description'])) {
98
                    $description             = $myrow['meta_description'];
99
                    $metas['og:description'] = $description;
100
                }
101
                if (!empty($myrow['image_file'])) {
102
                    $image_file        = $myrow['image_file'];
103
                    $metas['og:image'] = XOOPS_URL . '/uploads/' . $dir . '/' . $image_file;
104
                }
105
106
                return true;
107
            }
108
        }
109
    }
110
111
    return false;
112
}
113