Overview

Namespaces

  • None
  • swag

Classes

  • H5pSwagifact
  • H5pUtil
  • SettingsPageController
  • ShortcodeUtil
  • swag\ArrayUtil
  • swag\Singleton
  • swag\WpUtil
  • SwagMapController
  • SwagPageController
  • Swagpath
  • SwagpathController
  • SwagpathSyncer
  • SwagPlugin
  • SwagPostItem
  • SwagTgmpaController
  • SwagTrack
  • SwagTrackController
  • SwagUser
  • Template
  • Xapi
  • Overview
  • Namespace
  • Class
  1:   2:   3:   4:   5:   6:   7:   8:   9:  10:  11:  12:  13:  14:  15:  16:  17:  18:  19:  20:  21:  22:  23:  24:  25:  26:  27:  28:  29:  30:  31:  32:  33:  34:  35:  36:  37:  38:  39:  40:  41:  42:  43:  44:  45:  46:  47:  48:  49:  50:  51:  52:  53:  54:  55:  56:  57:  58:  59:  60:  61:  62:  63:  64:  65:  66:  67:  68:  69:  70:  71:  72:  73:  74:  75:  76:  77:  78:  79:  80:  81:  82:  83:  84:  85:  86:  87:  88:  89:  90:  91:  92:  93:  94:  95:  96:  97:  98:  99: 100: 101: 102: 103: 104: 105: 106: 107: 108: 109: 110: 111: 112: 113: 114: 115: 116: 117: 118: 119: 120: 121: 122: 123: 124: 125: 126: 127: 128: 129: 130: 131: 132: 133: 134: 135: 136: 137: 138: 139: 140: 141: 142: 143: 144: 145: 146: 147: 148: 149: 150: 151: 152: 153: 154: 155: 156: 157: 158: 159: 160: 161: 162: 163: 164: 165: 166: 167: 168: 169: 170: 171: 172: 173: 174: 175: 176: 177: 178: 179: 180: 181: 182: 183: 184: 185: 186: 187: 188: 189: 190: 191: 192: 193: 194: 195: 196: 197: 198: 199: 200: 201: 202: 203: 204: 205: 206: 207: 208: 209: 210: 211: 212: 213: 214: 215: 216: 217: 218: 219: 220: 221: 222: 223: 224: 225: 226: 227: 228: 229: 230: 231: 232: 233: 234: 235: 236: 237: 238: 239: 240: 241: 242: 243: 244: 245: 246: 247: 248: 249: 250: 251: 252: 253: 254: 255: 256: 257: 258: 259: 260: 261: 262: 263: 264: 265: 266: 267: 268: 269: 270: 271: 272: 273: 274: 275: 276: 277: 278: 279: 280: 281: 282: 283: 284: 285: 286: 287: 288: 289: 290: 291: 292: 293: 294: 295: 296: 297: 298: 299: 300: 301: 302: 303: 304: 305: 306: 307: 308: 309: 310: 311: 312: 313: 314: 315: 316: 317: 318: 319: 320: 321: 322: 323: 324: 325: 326: 327: 328: 329: 330: 331: 332: 333: 334: 335: 336: 337: 338: 339: 340: 341: 342: 343: 344: 345: 346: 347: 348: 349: 350: 351: 352: 353: 354: 355: 356: 357: 358: 359: 360: 361: 362: 363: 364: 365: 366: 367: 368: 369: 370: 371: 372: 373: 374: 375: 376: 377: 378: 379: 380: 381: 382: 383: 384: 385: 386: 
<?php

require_once __DIR__."/../utils/Singleton.php";
require_once __DIR__."/../model/SwagTrack.php";

use swag\Singleton;

/**
 * Controller for showing swag listing and swag map.
 */
class SwagPageController extends Singleton {

    /**
     * Init.
     */
    public function init() {
        if ($this->initCalled)
            return;

        $this->initCalled=true;

        register_post_type("swag",array(
            "labels"=>array(
                "name"=>"Swag",
            ),
            "public"=>true,
            "has_archive"=>true,
            "show_in_nav_menus"=>true,
            "show_ui"=>false,
            "has_archive"=>false
        ));

        add_action('pre_get_posts', array($this,'enableFrontPage'));
        add_filter('get_pages',array($this,'addSwagToDropDown'));

        add_shortcode("swagtoc", array($this,"swagtocShortcode"));
        add_shortcode("swagmap", array($this,"swagmapShortcode"));
        add_shortcode("my-swag",array($this,"mySwagShortcode"));
        add_shortcode("author-swag",array($this,"authorSwagShortcode"));
        add_shortcode("page-link",array($this,"pageLinkShortcode"));
        add_shortcode("login-user-author-link",array($this,"loginUserAuthorLinkShortcode"));

        add_shortcode("swag-view-test",array($this,"swagViewTestShortcode"));
    }

    public function loginUserAuthorLinkShortcode() {
        $u=wp_get_current_user();
        $url=get_author_posts_url($u->ID);

        return "<a href='$url'>$url</a>";
    }

    public function pageLinkShortcode($args) {
        $q=new WP_Query(array(
            "post_type"=>"page",
            "post_name"=>$args["slug"]
        ));

        $posts=$q->get_posts();
        $post=$posts[0];

        $title=$post->post_title;
        $url=get_post_permalink($post->ID);

        return "<a href='$url'>$title</a>";
    }

    /**
     * View test.
     */
    public function swagViewTestShortcode($args) {
        $template=new Template(__DIR__."/../../tpl/myswag.php");
        $template->set("pluginUrl",plugins_url()."/wp-swag/");

        $tracks=array(
            array(
                "name"=>"",
                "badges"=>array(
                    array(
                        "name"=>"What is Swag?",
                    ),
                )
            ),
            array(
                "name"=>"Technology",
                "score"=>"2 / 10",
                "badges"=>array(
                    array(
                        "name"=>"Programming",
                    ),
                    array(
                        "name"=>"Something else with a long name"
                    )
                )
            ),
            array(
                "name"=>"Design",
                "score"=>"3 / 15",
                "badges"=>array(
                    array(
                        "name"=>"GIMP",
                    ),
                    array(
                        "name"=>"Inkspace"
                    ),
                    array(
                        "name"=>"Photography"
                    )
                )
            )
        );

        $template->set("tracks",$tracks);

        return $template->render();
    }

    /**
     * Render the badge page for the given user.
     */
    public function renderBadgePage($swagUser, $args) {
        $topLevelTracks=get_terms(array(
            'taxonomy'=>'swagtrack',
            'parent'=>0
        ));

        $dummyTrack=new stdClass;
        $dummyTrack->name=NULL;
        $dummyTrack->slug=NULL;
        $dummyTrack->term_id=NULL;
        array_unshift($topLevelTracks,$dummyTrack);

        $tracks=array();
        foreach ($topLevelTracks as $topLevelTrack) {
            $swagTrack=SwagTrack::getById($topLevelTrack->term_id);
            if ($swagTrack)
                $color=$swagTrack->getDisplayColor();

            else
                $color=SwagTrack::DEFAULT_COLOR;

            $trackData=array(
                "name"=>$topLevelTrack->name,
                "color"=>$color,
                "badges"=>array(),
                "score"=>"",
            );

            $swagpaths=$swagUser->getCompletedByTopLevelTrack($topLevelTrack->slug);
            foreach ($swagpaths as $swagpath) {
                $trackData["badges"][]=array(
                    "name"=>$swagpath->getPost()->post_title
                );
            }

            $allForTrack=Swagpath::findAllForTopLevelTrack($topLevelTrack->slug);

            $trackData["score"]=sizeof($swagpaths)." / ".sizeof($allForTrack);
            $tracks[]=$trackData;
        }

        $template=new Template(__DIR__."/../../tpl/myswag.php");
        $template->set("pluginUrl",plugins_url()."/wp-swag/");
        $template->set("tracks",$tracks);
        $template->set("avatar",get_avatar($swagUser->getId(),75));
        $template->set("swagCount",sizeof($swagUser->getCompletedSwagpaths()));
        $template->set("username",$swagUser->getUser()->display_name);

        return $template->render();
    }

    /**
     * Show swag for current author.
     */
    public function authorSwagCountShortcode($args) {
        $swagUser=SwagUser::getById(get_the_author_meta('ID'));
        return sizeof($swagUser->getCompletedSwagpaths());
    }

    /**
     * Show swag for current author.
     */
    public function authorSwagShortcode($args) {
        global $wp_query;

        $user=get_user_by("login",$wp_query->query["author_name"]);
        $swagUser=SwagUser::getById($user->ID);

        return $this->renderBadgePage($swagUser,$args);
    }

    /**
     * My Swag.
     */
    public function mySwagShortcode($args) {
        $swagUser=SwagUser::getCurrent();
        return $this->renderBadgePage($swagUser,$args);
    }

    public function swagmapShortcode($args) {
        $mode="my";
        if (isset($_REQUEST["mode"]))
            $mode=$_REQUEST["mode"];

        $q=new WP_Query(array(
            "name"=>"map",
            "post_type"=>"swag"
        ));

        $posts=$q->get_posts();
        if (sizeof($posts)!=1)
            throw new Exception("Expected one post with a swagmap, found=".sizeof($posts));

        $url=get_permalink($posts[0]);

        $template=new Template(__DIR__."/../../tpl/swagmap.php");
        $template->set("mode",$mode);
        $template->set("plugins_uri",plugins_url()."/wp-swag");
        $template->set("mylink",$url."?mode=my");
        $template->set("fulllink",$url."?mode=full");
        $template->show();
    }

    public function addSwagToDropDown($pages) {
        $args = array(
            'post_type' => 'swag'
        );
        $items = get_posts($args);
        $pages = array_merge($pages, $items);

        return $pages;
    }

    public function enableFrontPage($query) {
        if('' == $query->query_vars['post_type'] && 0 != $query->query_vars['page_id'])
            $query->query_vars['post_type'] = array( 'page', 'swag' );
    }

    /**
     * Does this post exist.
     */
    private static function getPostId($type, $slug) {
        global $wpdb;

        $q=$wpdb->prepare(
            "SELECT ID ".
            "FROM   {$wpdb->prefix}posts ".
            "WHERE  post_type=%s ".
            "AND    post_name=%s ",
            $type,$slug);
        $id=$wpdb->get_var($q);

        if ($wpdb->last_error)
            throw new Exception($wpdb->last_error);

        return $id;
    }

    /**
     * Install.
     */
    public function install() {
        $this->init();

        $postId=SwagPageController::getPostId("swag","toc");
        if (!$postId) {
            $postId=wp_insert_post(array(
                "post_type"=>"swag",
                "post_name"=>"toc",
                "post_title"=>"Swag Table of Contents",
                "post_status"=>"publish",
                "post_content"=>"[swagtoc]",
            ));

            if (!$postId)
                throw new Exception("Unable to create post");
        }

        $postId=SwagPageController::getPostId("swag","map");
        if (!$postId) {
            $postId=wp_insert_post(array(
                "post_type"=>"swag",
                "post_name"=>"map",
                "post_title"=>"Swagmap",
                "post_status"=>"publish",
                "post_content"=>"[swagmap]",
            ));

            if (!$postId)
                throw new Exception("Unable to create post");
        }
    }

    /**
     * Used when sorting swagpaths, so unprepared comes last.
     */
    private static function cmpSwagpathViewData($a, $b) {
        if ($a["prepared"] && !$b["prepared"])
            return -1;

        if (!$a["prepared"] && $b["prepared"])
            return 1;

        return 0;
    }

    /**
     * Render the table of contents.
     */
    public function swagtocShortcode($args) {
        $url=get_permalink();

        if (isset($_REQUEST["track"]) && $_REQUEST["track"]) {
            $parentTrack=SwagTrack::getBySlug($_REQUEST["track"]);
            $parentTrackId=$parentTrack->getId();
        }

        else {
            $parentTrack=NULL;
            $parentTrackId=NULL;
        }

        $tracks=SwagTrack::getByParentId($parentTrackId);
        $trackViews=array();
        foreach ($tracks as $track) {
            $trackViews[]=array(
                "title"=>$track->getTerm()->name,
                "description"=>$track->getTerm()->description,
                "url"=>$url."?track=".$track->getTerm()->slug,
                "color"=>$track->getDisplayColor()
            );
        }

        $unprepared=0;
        $swagpaths=Swagpath::getByTrackId($parentTrackId);
        $swagpathViews=array();
        foreach ($swagpaths as $swagpath) {
            $swagpathViews[]=array(
                "title"=>$swagpath->getPost()->post_title,
                "description"=>$swagpath->getPost()->post_excerpt,
                "url"=>get_permalink($swagpath->getId()),
                "prepared"=>$swagpath->isCurrentUserPrepared(),
                "complete"=>$swagpath->isCompletedByCurrentUser(),
                "color"=>$swagpath->getDisplayColor(),
            );

            if (!$swagpath->isCurrentUserPrepared())
                $unprepared++;
        }

        usort($swagpathViews,"SwagPageController::cmpSwagpathViewData");

        $trail=array();
        $ancestors=get_ancestors($parentTrackId,"swagtrack","taxonomy");
        if ($parentTrackId)
            array_unshift($ancestors,$parentTrackId);

        $ancestors=array_reverse($ancestors);

        $trail[]=array(
            "title"=>"Tracks",
            "url"=>$url
        );

        foreach ($ancestors as $ancestorId) {
            $ancestor=get_term($ancestorId);
            $item=array();
            $item["url"]=$url."?track=".$ancestor->slug;
            $item["title"]=$ancestor->name;

            $trail[]=$item;
        }

        if (sizeof($trail)<2)
            $trail=array();

        $template=new Template(__DIR__."/../../tpl/toc.php");
        $template->set("pluginurl",plugins_url()."/wp-swag");
        $template->set("tracks",$trackViews);
        $template->set("swagpaths",$swagpathViews);
        $template->set("unprepared",$unprepared);
        $template->set("trail",$trail);

        return $template->render();
    }
}
Swag API documentation generated by ApiGen