-
Notifications
You must be signed in to change notification settings - Fork 1
/
plugin.cfc
97 lines (75 loc) · 3.83 KB
/
plugin.cfc
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
<cfcomponent extends="BasePlugin">
<cffunction name="init" access="public" output="false" returntype="any">
<cfargument name="mainManager" type="any" required="true">
<cfargument name="preferences" type="any" required="true">
<cfscript>
setManager(arguments.mainManager);
setPreferencesManager(arguments.preferences);
setPackage("com.tysonkelley.plugins.metaTags");
variables.metaTags = structNew();
// Map our meta tags
variables.metaTags["metaTags_description"] = "";
variables.metaTags["metaTags_keywords"] = "";
return this;
</cfscript>
</cffunction>
<cffunction name="processEvent" hint="Synchronous event handling" access="public" output="false" returntype="any">
<cfargument name="event" type="any" required="true">
<cfscript>
var local = StructNew();
// Run event handler
switch(arguments.event.name){
case "beforeHtmlHeadEnd": return displayMetaTags(arguments.event);
case "beforeAdminPostFormDisplay": return removeMetaTagsFromCustomFields(arguments.event);
case "beforeAdminPostFormEnd": return displayMetaTagFields(arguments.event);
case "beforePostAdd": return addMetaTagsToCustomFields(arguments.event);
case "beforePostUpdate": return addMetaTagsToCustomFields(arguments.event);
}
return arguments.event;
</cfscript>
</cffunction>
<cffunction name="removeMetaTagsFromCustomFields" hint="Take our meta tags out of the custom fields (we'll use a custom display instead)" access="private" output="false" returntype="any">
<cfargument name="event" type="any" required="true">
<cfset var local = StructNew()>
<cfloop collection="#variables.metaTags#" item="local.key">
<cfif arguments.event.item.customFieldExists(local.key)>
<cfset variables.metaTags[local.key] = arguments.event.item.getCustomField(local.key).value>
<cfset arguments.event.item.removeCustomField(local.key)>
</cfif>
</cfloop>
<cfreturn arguments.event>
</cffunction>
<cffunction name="displayMetaTagFields" hint="Use a custom display for the meta tags" access="private" output="false" returntype="any">
<cfargument name="event" type="any" required="true">
<cfset var local = StructNew()>
<cfset local.metaKeywords = variables.metaTags.metaTags_keywords>
<cfset local.metaDescription = variables.metaTags.metaTags_description>
<cfsavecontent variable="contentForDisplay"><cfinclude template="form.cfm"></cfsavecontent>
<cfset arguments.event.setOutputData(arguments.event.getOutputData() & contentForDisplay)>
<cfreturn arguments.event>
</cffunction>
<cffunction name="displayMetaTags" hint="Output the actual meta tags" access="private" output="false" returntype="any">
<cfargument name="event" type="any" required="true">
<cfset var local = StructNew()>
<cfset local.metaTags = StructNew()>
<cfset local.currentPost = arguments.event.contextData.currentPost>
<cfloop collection="#variables.metaTags#" item="local.key">
<cfif local.currentPost.customFieldExists(local.key)>
<cfset local.metaTags[local.key] = local.currentPost.getCustomField(local.key).value>
</cfif>
</cfloop>
<cfsavecontent variable="contentForDisplay"><cfinclude template="display.cfm"></cfsavecontent>
<cfset arguments.event.setOutputData(arguments.event.getOutputData() & contentForDisplay)>
<cfreturn arguments.event>
</cffunction>
<cffunction name="addMetaTagsToCustomFields" hint="Add the meta tags back into custom fields" access="private" output="false" returntype="any">
<cfargument name="event" type="any" required="true">
<cfset var local = StructNew()>
<cfloop collection="#variables.metaTags#" item="local.key">
<cfif structKeyExists(arguments.event.data.rawData, local.key)>
<cfset arguments.event.data.post.setCustomField(local.key, local.key, arguments.event.data.rawdata[local.key])>
</cfif>
</cfloop>
<cfreturn arguments.event>
</cffunction>
</cfcomponent>