-
Notifications
You must be signed in to change notification settings - Fork 0
/
BadgeosShowSub_Plugin.php
157 lines (116 loc) · 4.75 KB
/
BadgeosShowSub_Plugin.php
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
<?php
include_once('BadgeosShowSub_LifeCycle.php');
class BadgeosShowSub_Plugin extends BadgeosShowSub_LifeCycle {
/**
* See: http://plugin.michael-simpson.com/?page_id=31
* @return array of option meta data.
*/
public function getOptionMetaData() {
// http://plugin.michael-simpson.com/?page_id=31
return array(
//'_version' => array('Installed Version'), // Leave this one commented-out. Uncomment to test upgrades.
'ATextInput' => array(__('Enter in some text', 'my-awesome-plugin')),
'AmAwesome' => array(__('I like this awesome plugin', 'my-awesome-plugin'), 'false', 'true'),
'CanDoSomething' => array(__('Which user role can do something', 'my-awesome-plugin'),
'Administrator', 'Editor', 'Author', 'Contributor', 'Subscriber', 'Anyone')
);
}
protected function initOptions() {
$options = $this->getOptionMetaData();
if (!empty($options)) {
foreach ($options as $key => $arr) {
if (is_array($arr) && count($arr > 1)) {
$this->addOption($key, $arr[1]);
}
}
}
}
public function getPluginDisplayName() {
return 'BadgeOS Show Submission Add-on';
}
protected function getMainPluginFileName() {
return 'badgeos-show-submission-add-on.php';
}
/**
* Perform actions when upgrading from version X to version Y
* See: http://plugin.michael-simpson.com/?page_id=35
* @return void
*/
public function upgrade() {
}
public function addActionsAndFilters() {
add_action('admin_menu', array(&$this, 'addSettingsSubMenuPage'));
add_filter('badgeos_render_achievement', array($this, 'addLinkToSubmission'), 10, 2);
add_filter('the_content', array($this, 'removeBadgeThumbnail'), 11);
add_filter('badgeos_get_submission_attachments', array($this, 'swapWordAttachmentProof'), 11);
}
public function removeBadgeThumbnail($content) {
$postType = get_post_type();
if($postType == 'badges') {
$content = preg_replace('#<div class="alignleft badgeos-item-image">(.*?)</div>#', '', $content);
}
return $content;
}
public function swapWordAttachmentProof($content) {
$content = str_replace('Submitted Attachments:', 'Submitted evidence:', $content);
$content = str_replace('Pièces-Jointes Soumises :', ' Preuves soumises:', $content);
return $content;
}
public function addLinkToSubmission($output, $achivementID) {
$displayedID = bp_displayed_user_id();
if(empty($displayedID)) {
return $output;
}
/*
* Now we take care of removing the thumbnail as requested by client
*/
$doc = new DOMDocument();
#self::setErrorHandler();
$doc->loadHTML (mb_convert_encoding($output, 'HTML-ENTITIES', 'UTF-8'));
#self::setErrorHandler(TRUE);
$xpath = new \DOMXPath ( $doc );
/*
* Du parent div ID = badgeos-achievements-container,
* les enfants qui ont la classe badgeos-item-image
* From http://stackoverflow.com/questions/11686287/php-xml-removing-element-and-all-children-according-to-node-value
*/
$q = '//div[@class="badgeos-item-image"]';
$thumbnailDiv = $xpath->query($q);
$parentNode = $thumbnailDiv->item(0)->parentNode;
$parentNode->removeChild($thumbnailDiv->item(0));
$q = '//div[@class="badgeos-item-description"]';
$descDiv = $xpath->query($q)->item(0);
$descDiv->setAttribute("style", "width:100%");
$q = '//h2[@class="badgeos-item-title"]';
$descDiv = $xpath->query($q)->item(0);
$descDiv->setAttribute("style", "font-size:140%;margin-bottom:0.5cm");
$strReturn = $doc->saveHTML();
#$strReturn = $output;
/*
* According to badgeos_parse_feedback_args
* 'status'=> 'auto-approved' shows both approved & auto-approved
*/
$args = array(
'author' => $displayedID,
'achievement_id' => $achivementID,
'post_type' => 'submission',
'show_attachments' => true,
'show_comments' => true,
'status' => 'auto-approved',
'numberposts' => 1,
'suppress_filters' => false,
);
$args = badgeos_parse_feedback_args($args);
$wpq = new WP_Query;
$posts = $wpq->query($args);
$submissionID = $posts[0]->ID;
if(empty($submissionID)) {
return $strReturn;
}
$linkURL = get_permalink($submissionID, false);
$divToAdd = "<!-- BEGIN Show detail link for ach # $achivementID, author # $displayedID, submission postID # $submissionID --><div>" . '<a href="' . $linkURL . '">' . translate('Show Details', 'badgeos') .'</a></div><!-- END Show detail link -->';
$htmlCommentMarker = '<!-- .badgeos-item-excerpt -->';
$strReturn = str_replace($htmlCommentMarker, $htmlCommentMarker . $divToAdd, $strReturn);
return $strReturn;
}
}